我的目标是:给定的二元反应(如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")
任何想法,将非常感激!
每次发布信息时,这让我很沮丧,我还没有来得及学会data.table但 – alexwhan 2013-03-16 10:56:57
@alexwhan,你的'ddply'解决方案可能只是:'> ddply(数据,(总),总结,N = sum(x1 == 0),y = sum(x1 == 1))'no?为什么不尝试'data.table'解决方案?如果有改进,我可以根据我的知识来纠正你的问题...... – Arun 2013-03-16 13:55:03
我打算将其标记为已解决,尽管我希望找到一个不需要外部软件包(如data.table,reshape2,或者plyr)。感谢您的帮助! – 2013-03-22 00:49:27