0

R 版本 3.3.2

我正在尝试从 xtabs 列表中获取单个数据框。到目前为止,我已经成功地组合了多个 xtab,但我还没有成功地将其转换为数据框。此外,我想知道是否可以在所述数据框中包含 xtabs 的暗名,以分隔不同的 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)

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

            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

我尝试了很多方法并阅读了我能找到的所有内容,但我找不到让它工作的方法,尤其是在访问暗名时(即“结果”、“问题 1”、“问题 2”等)。任何帮助是极大的赞赏!

4

0 回答 0