R - 将多个xtabs转换为数据帧

时间:2017-06-23 00:45:49

标签: r dataframe rstudio lapply contingency

R版本3.3.2

我试图从xtabs列表中获取单个数据帧。到目前为止,我已经设法将多个xtabs组合在一起,但我还没有成功地将其转换为数据框。此外,我想知道是否可以在所述数据框中包含xtabs的dimnames,将不同的xtabs分开。

这是一个可复制的例子:

id <- c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7", "ID8", "ID9", "ID10", "ID11", "ID12", "ID13", "ID14", "ID15")
outcome <- c(1, 1, 1, 1, 0, "FP", 0, 1, 1, "FP", 0, 1, "FP", 0, 1)
question1 <- c("No", "No", "Sometimes", "Yes", "Yes", "Yes", "Yes", "Sometimes", "Yes", "No", "No", "Yes", "Yes", "Yes", "No")
question2 <- c("NYC", "LA", "LA", "NYC", "NYC", "NYC", "NYC", "NYC", "NYC", "LA", "NYC", "NYC", "NYC", "NYC", "NYC")
question3 <- c("Yes", "No", "Yes", "Yes", "Yes", "No", "Yes", "No", "No", "No", "No", "Yes", "Yes", "No", "No")
question4 <- c("Nurse", "Neurologist", "Oncologist", "Internist", "Nurse", "Neurologist", "Nurse", "Nurse", "Oncologist", "Nurse", "Nurse", "Internist", "Nurse", "Neurologist", "Nurse")
question5 <- c("Female", "Male", "Female", "Female", "Male", "Female", "Male", "Male", "Male", "Female", "Male", "Female", "Male", "Male", "Male")
question6 <- c(42, 35, 39, 32, 40, 51, 37, 42, 33, 41, 55, 53, 45, 38, 52)

df <- data.frame(id, outcome, question1, question2, question3, question4, question5, question6)

为了获得列联表的列表,我写了以下内容:

bar <- lapply(colnames(subset(df, select = -c(id, outcome))),

              function(var) {

                  formula <- as.formula(paste("~", var, "+ outcome"))

                  xtabs(formula, data = df)
              })

然后,为了从该列表中获取数据帧,我使用了ldply:

ldply(colnames(subset(df, select = -c(id, outcome))),

      function(var) {

        formula <- as.formula(paste("~", var, "+ outcome"))

        xtabs(formula, data = df)
      })

不幸的是,由于我丢失了流程中变量的名称,因此对我无效。 然后我尝试绑定列表的元素并将其转换为数据框:

bar2 <- bar[[1]]
lapply(2:length(bar), function(i) {bar2 <<- rbind(bar2, bar[[i]])})
data.frame(bar2)

出现两个问题:我收到一条警告信息并再次丢失变量的名称,我仍然无法找到在答案(行变量)之间包含dimnames的方法。我希望我的最终数据框看起来像这样:

            outcome 

            0 1 FP
question1
No          1 3  1
Sometimes   0 2  0
Yes         3 3  2
question2
LA          0 2  1
NYC         4 6  2
question3
No          2 4  2
Yes         2 4  1
question4
Internist   0 2  0
Neurologist 1 1  1
Nurse       3 3  2
Oncologist  0 2  0
question5
Female      0 4  2
Male        4 4  1
question6
32          0 1  0
33          0 1  0
35          0 1  0
37          1 0  0
38          1 0  0
39          0 1  0
40          1 0  0
41          0 0  1
42          0 2  0
45          0 0  1
51          0 0  1
52          0 1  0
53          0 1  0
55          1 0  0

我尝试了很多方法并阅读了我能找到的所有内容,但我无法找到一种方法让它发挥作用,特别是在访问dimnames时(即&#39;结果&#39;,&# 39;问题1&#39;,&#39;问题2&#39;等)。非常感谢任何帮助!

0 个答案:

没有答案