2014-10-10 98 views
0

我有一个分类数据集,我试图总结说,这些数据集在被问到的问题的性质上存在内在的差异。下面的数据代表了一个有标准封闭式问题的调查问卷,也是一个可以从列表中选择多个答案的问题。 “村庄”和“收入”代表了封闭的问题。 “负责人1”等......代表被访者对每个人说“是”或“否”的列表。重新格式化R中的分类数据

VILLAGE INCOME   responsible.1 responsible.2 responsible.3 responsible.4 responsible.5 
    j  both   DLNR    NA    DEQ    NA   Public 
    k  regular.income DLNR    NA    NA    NA   NA 
    k  regular.income DLNR    CRM    DEQ    Mayor  NA 
    l  both   DLNR    NA    NA    Mayor  NA 
    j  both   DLNR    CRM    NA    Mayor  NA 
    m  regular.income DLNR    NA    NA    NA   Public 

我要的是“村”的缠绕成ftable“负责任的”负责任的变量的一套3路表的输出。这样,我可以使用包含多个R包的表格进行图形和分析。

 RESPONSIBLE    
VILLAGE INCOME   responsible.1 responsible.2 responsible.3 responsible.4 responsible.5 
j  both   2    1    1    1    1 
k  regular income 2    1    1    1    0 
l  both   1    0    0    1    0 
m  regular income 1    0    0    0    1 

as.data.frame(table(village, responsible.1)会得到我的第一次,但我无法弄清楚如何获得一个不错的ftable结束了整个事情。

+0

是第二个表显示了预期的结果? – 2014-10-10 21:01:15

+0

是的,第二个表具有“RESPONSIBLE”作为具有五级答案的变量名称。其他两个变量是不言自明的。你还可以建议如何将我们的“整洁”表粘贴到评论板以避免刚刚发生的事情? – 2014-10-10 21:17:59

+0

您可以通过单击“{}”图标来格式化问题中的代码 – 2014-10-10 21:18:53

回答

1
> aggregate(dat[-(1:2)], dat[1:2], function(x) sum(!is.na(x))) 
    VILLAGE   INCOME responsible.1 responsible.2 responsible.3 responsible.4 responsible.5 
1  j   both    2    1    1    1    1 
2  l   both    1    0    0    1    0 
3  k regular.income    2    1    1    1    0 
4  m regular.income    1    0    0    0    1 

我猜你实际上有另一个分组矢量,也许是第一个“负责任”的列?

我真的不明白的排序规则,但扭转了分组列的顺序可能是更接近你贴:

> aggregate(dat[-(1:2)], dat[2:1], function(x) sum(!is.na(x))) 
      INCOME VILLAGE responsible.1 responsible.2 responsible.3 responsible.4 responsible.5 
1   both  j    2    1    1    1    1 
2 regular.income  k    2    1    1    1    0 
3   both  l    1    0    0    1    0 
4 regular.income  m    1    0    0    0    1 
+0

这看起来不错,但我怎样才能把它变成一个ftable(即3-way表,适用于分类图和loglm)?会喜欢“负责任的1等等”,包裹在一个变量“RESPONSIBLE”中,该变量包含在3-way ftable中。 – 2014-10-10 21:31:26

+0

如果您希望看到针对其他数据问题的解决方案,请发布一个提供足够的复杂性以请求修正的示例。 – 2014-10-10 21:34:03

+0

请不要使用“etc”作为请求的描述。我在问题陈述中对这个缩写形成了强烈的过敏反应。 – 2014-10-10 21:38:09