R中交叉路口一侧的多个变量

时间:2014-08-06 21:20:46

标签: r crosstab

我有一个如下所示的数据集:

ID  wts     S2      S5.1    S5.2    S5.3
42  0.78    Male    Yes     No      Yes
45  1.22    Female  No      Yes     No
48  0.98    Male    Yes     Yes     Yes
49  1.11    Female  Yes     Yes     No
51  1.21    Male    Yes     Yes     No

我正在尝试使用'descr'包中的'crosstab'函数创建一个加权表。我有一个基本表正常工作,使用以下行:

crosstab(*fileName*$S5.1,*fileName*$S2,weight=wts,prop.c=T) 

但我真正想做的是将所有3个二进制变量放在表格的y轴上。我环顾四周,无法弄明白。任何帮助,将不胜感激!如果该信息,我通过SPSS文件提取数据。帮助

如果这是一个非常简单的问题,请道歉。对R来说很新。


dput(head(data))


structure(list(ID = c(42, 45, 48, 49, 51), wts = c(0.78, 1.22, 
0.98, 1.11, 1.21), S2 = structure(c(1L, 2L, 1L, 2L, 1L), .Label = c("Male", 
"Female"), class = "factor"), S5.1 = structure(c(2L, 1L, 2L, 
2L, 2L), .Label = c("No", "Yes"), class = "factor"), S5.2 = structure(c(1L, 
2L, 2L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"), 
S5.3 = structure(c(2L, 1L, 2L, 1L, 1L), .Label = c("No", 
"Yes"), class = "factor")), .Names = c("ID", "wts", "S2", 
"S5.1", "S5.2", "S5.3"), variable.labels = structure(c("", "", 
"Gender", "Dog?", "Cat?", "Bird?"), .Names = c("ID", "wts", "S2", 
"S5.1", "S5.2", "S5.3")), codepage = 1252L, row.names = c(NA, 
5L), class = "data.frame")

我希望得到类似的东西......(第2和第3个是/否分别添加了S5.2和S5.3)。谢谢!

==========================================
                  temp.spss$S2
temp.spss$S5.1       Male   Female   Total
------------------------------------------
No                      0        1       1
                    0.000   50.000        
------------------------------------------
Yes                     3        1       4
                  100.000   50.000        
------------------------------------------
No                      1        0       1
                    33.333    00.000        
------------------------------------------
Yes                     2        2       4
                    66.666  100.000        
------------------------------------------
No                      1        2       3
                    33.333   100.000        
------------------------------------------
Yes                     2        0       2
                   66.666    0.000        
------------------------------------------
Total                   9        6       15
                   180.000   120.000
==========================================

1 个答案:

答案 0 :(得分:0)

也许正在使用互动(并猜测你不希望所有零条目的行:

install.packages("descr")
 descr::crosstab( with(dat, 
   interaction(S5.1, S5.2, S5.3, drop=TRUE)) ,dat$S2,weight=dat$wts,prop.c=T)

在查看结果后,我发现需要审查敏感性的结果。

#+-------------------------------------------------------
   Cell Contents 
|-------------------------|
|                   Count | 
|          Column Percent | 
|-------------------------|

================================================================================
                                                         dat$S2
with(dat, interaction(S5.1, S5.2, S5.3, drop = TRUE))    Female     Male   Total
--------------------------------------------------------------------------------
No.Yes.No                                                     1        0       1
                                                         50.000    0.000        
--------------------------------------------------------------------------------
Yes.Yes.No                                                    1        1       2
                                                         50.000   33.333        
--------------------------------------------------------------------------------
Yes.No.Yes                                                    0        1       1
                                                          0.000   33.333        
--------------------------------------------------------------------------------
Yes.Yes.Yes                                                   0        1       1
                                                          0.000   33.333        
--------------------------------------------------------------------------------
Total                                                         2        3       5
                                                         40.000   60.000
================================================================================
>