How to subset data for a specific column with ddply?

别说谁变了你拦得住时间么 提交于 2019-12-01 21:15:39

With plyr, you can do it as follows:

ddply(df,
      .(Condition), summarise, 
      N          = length(Response),
      nAccurate  = sum(Accuracy),
      RT         = mean(RT[Accuracy==1]))

this gives:

   Condition N nAccurate     RT
1:         1 6         4 127.50
2:         2 6         4 300.25

If you use data.table, then this is an alternative way:

library(data.table)
setDT(df)[, .(N = .N,
              nAccurate = sum(Accuracy),
              RT = mean(RT[Accuracy==1])),
          by = Condition]
Andriy T.

Using dplyr package:

library(dplyr)
df %>%
  group_by(Condition) %>%
  summarise(N = n(),
            nAccurate = sum(Accuracy),
            RT = mean(RT[Accuracy == 1]))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!