2012-10-11 106 views
0

我有一个数据帧具有以下结构,dput(scoreDF)计算在R上的平均值,标准误差和%的数据帧

scoreDF <- structure(list(ID = c(1, 2), Status = structure(c(2L, 1L), 
    .Label = c("Fail", "Pass"), class = "factor"), Subject_1_Score = c(100, 25), 
    Subject_2_Score = c(50, 76)), .Names = c("ID", "Status", "Subject_1_Score", 
    "Subject_2_Score"), row.names = c(NA, -2L), class = "data.frame") 

现在,我需要拿出谁通过和学生%失败,意味着通过和失败的学生,标准误差相同。

stdErr <- function(x) {sd(x)/ sqrt(length(x))} 

其中我期望x成为其标准误差需要计算一个矢量:

对于标准错误,我已经定义如下的函数。

我看过ddply的文档,但我无法弄清楚如何计算上面的数据帧的%(即通过次数)/(总计数)和标准错误。

+1

这不是一个可重复的问题。见例如http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example获得灵感。 –

+1

如果我理解你的问题,不需要'plyr'。 'nrow(数据[数据$状态== '合格',])/ nrow(数据)'。除非你想分割'ID' ...'ddply(数据,。(ID),汇总,总和(状态=='通过')/长度(状态)' – Justin

+0

@Justin:我希望能够提出一种我不需要硬编码值的方法,比如'Status =='Pass'',这就是为什么我试图用'ddply'找到某些东西的原因。是否可以通过'Status'而不是'ID ' –

回答

3

您可以使用tapply来计算组统计信息。如果您的数据帧被称为学生随后通过传球计算平均/失败应指定:

tapply(students$Subject_1_Score, students$Status, FUN=mean) 

标准错误代替你STDERR功能平均值。

如果要计算在多个列的东西,你可以索引x:

tapply(students[,2:3], students$Status, FUN=mean) 

来计算传递%的学生:按分数

dim(students[students$Status == "Pass" ,])[1]/dim(students)[1] 

或者:

dim(students[students$Subject_1_Score >= 65 ,])[1]/dim(students)[1] 

以上是使用索引的此类矢量语句的数据框示例:

length(x[x == "Pass"])/length(x) 

要计算跨行或列的函数,您可以使用apply

+0

'标准错误代替你的stdErr函数的意思.' ..但他们是不是同样的权利? –

+0

FUN如果你想使用你的stdErr函数:tapply(students $ Subject_1_Score,students $ Status,FUN = stdErr) –

相关问题