我的目标是:给定一个二分响应的数据框(例如,0 和 1),我如何生成一个汇总矩阵:1)有两列(一列用于正确回答第一个问题,另一列用于错误回答), 2) 具有与获得特定总分的个人数量有关的行。
例如,假设我有 50 个受访者和 5 个问题。这意味着有 6 种响应模式(全部不正确/0,然后是 1、2、3 和 4 正确,最后全部正确/1)。我希望得到的矩阵对象看起来像:
... INCORRECT ..... CORRECT <-- pertaining to a 0 or 1 on the first item respectively
[1]... 10 ............ 0 <-- indicating people who, after responded 0 on the first question, responded 0 on all questions (5 zeroes)
[2]... 8 ............ 2 <-- indicating 12 people who got 1 correct (8 got the first question incorrect, 2 got the first question correct)
[3]... 4 ............. 8 <-- indicating 12 people who got 2 correct (4 got the first question incorrect but got 2 of the other questions correct, 8 got the first question and 1 other correct)
[4]... 6 ............. 3 <-- indicating 9 people who got 3 correct
[5]... 3 ............. 4 <-- indicating 7 people who got 4 correct
[6]... 0 ............. 8 <-- pertaining to the 8 people who answered all 5 questions correctly (necessarily indicating they got the first question correct).
我的思路是,我需要按第一个问题的表现(一次工作一列)拆分数据框,并找到每一行(参与者)的总分,然后将它们列在第一列中;然后对第二个做同样的事情?
这将被构建到一个包中,所以我试图弄清楚如何只使用基本函数来做到这一点。
这是一个类似于我将使用的示例数据集:
n <- 50
z <- c(0, 1)
samp.fun <- function(x, n){
sample(x, n, replace = TRUE)
}
data <- data.frame(0)
for (i in 1:5){
data[1:n, i] <- samp.fun(z, n)
}
names(data)[1:5] <- c("x1", "x2", "x3", "x4", "x5")
任何想法将不胜感激!