How to subset data for a specific column with ddply?

后端 未结 2 881
余生分开走
余生分开走 2021-01-20 23:41

I would like to know if there is a simple way to achieve what I describe below using ddply. My data frame describes an experiment with two conditions. Participants

相关标签:
2条回答
  • 2021-01-20 23:58

    Using dplyr package:

    library(dplyr)
    df %>%
      group_by(Condition) %>%
      summarise(N = n(),
                nAccurate = sum(Accuracy),
                RT = mean(RT[Accuracy == 1]))
    
    0 讨论(0)
  • 2021-01-21 00:06

    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]
    
    0 讨论(0)
提交回复
热议问题