如何使用ddply对特定列的数据进行子集化

如何使用ddply对特定列的数据进行子集化

本文介绍了如何使用ddply对特定列的数据进行子集化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法可以使用ddply实现我在下面描述的内容.我的数据框描述了具有两个条件的实验.参与者必须在选项 A B 之间进行选择,我们记录了他们决定所需的时间以及他们的回答是否准确.

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 had to select between options A and B, and we recorded how long they took to decide, and whether their responses were accurate or not.

我使用ddply根据条件创建平均值. nAccurate列总结了每种情况下的准确响应次数.我也想知道他们花了多少时间来决定和表达在RT栏中.但是,我只想计算参与者正确的响应时间(即Accuracy==1),才能计算出平均响应时间.当前,下面的代码只能计算所有响应(准确和不准确的响应)的平均反应时间.是否有一种简单的方法可以对其进行修改,以获取仅在准确的试验中计算出的平均响应时间?

I use ddply to create averages by condition. The column nAccurate summarizes the number of accurate responses in each condition. I also want to know how much time they took to decide and express it in the column RT. However, I want to calculate average response times only when participants got the response right (i.e. Accuracy==1). Currently, the code below can only calculate average reaction times for all responses (accurate and inaccurate ones). Is there a simple way to modify it to get average response times computed only in accurate trials?

请参见下面的示例代码,谢谢!

See sample code below and thanks!

library(plyr)

# Create sample data frame.
Condition = c(rep(1,6), rep(2,6))                               #two conditions
Response  = c("A","A","A","A","B","A","B","B","B","B","A","A")  #whether option "A" or "B" was selected
Accuracy  = rep(c(1,1,0),4)                                     #whether the response was accurate or not
RT        = c(110,133,121,122,145,166,178,433,300,340,250,674)  #response times
df        = data.frame(Condition,Response, Accuracy,RT)

head(df)

  Condition Response Accuracy  RT
1         1        A        1 110
2         1        A        1 133
3         1        A        0 121
4         1        A        1 122
5         1        B        1 145
6         1        A        0 166

# Calculate averages.
avg <- ddply(df, .(Condition), summarise,
                 N          = length(Response),
                 nAccurate  = sum(Accuracy),
                 RT         = mean(RT))

# The problem: response times are calculated over all trials. I would like
# to calculate mean response times *for accurate responses only*.

avg
  Condition N nAccurate       RT
          1 6         4 132.8333
          2 6         4 362.5000

推荐答案

使用plyr,您可以执行以下操作:

With plyr, you can do it as follows:

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

这给出了:

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

如果使用data.table,则这是另一种方法:

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]

这篇关于如何使用ddply对特定列的数据进行子集化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:32