[英]I need Multiple choice questions with cross tabulations including Totals in R Programming. Below is what I am looking for
在R中有很多交叉表的方法。
一個解法:
library(tidyverse)
data <- data.frame(
q1 = sample(x = c("yes", "no"), size = 10, replace = TRUE),
q2_1 = sample(x = 0:1, size = 10, replace = TRUE),
q2_2 = sample(x = 0:1, size = 10, replace = TRUE),
q2_3 = sample(x = 0:1, size = 10, replace = TRUE),
q2_4 = sample(x = 0:1, size = 10, replace = TRUE)
)
head(data)
q1 q2_1 q2_2 q2_3 q2_4
1 yes 0 0 0 0
2 yes 0 0 1 1
3 no 1 1 0 1
4 no 0 0 0 0
5 no 1 0 1 0
6 no 1 1 0 0
#long format data
longdata <- gather(data,key = "q2","value" ,-q1)
ftable(longdata)
value 0 1
q1 q2
no q2_1 4 3
q2_2 3 4
q2_3 5 2
q2_4 5 2
yes q2_1 2 1
q2_2 2 1
q2_3 1 2
q2_4 1 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.