How to subset data for a specific column with ddply? <p>I would like to know if there is a simple way to achieve what I describe below using <code>ddply</code>. My data frame describes an experiment with two conditions. Participants had to select between options <em>A</em> and <em>B</em>, and we recorded how long they took to decide, and whether their responses were accurate or not. </p> <p>I use <code>ddply</code> to create averages by condition. The column <code>nAccurate</code> 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 <code>RT</code>. However, I want to calculate average response times <strong>only when participants got the response right</strong> (i.e. <code>Accuracy==1</code>). 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?</p> <p>See sample code below and thanks!</p> <pre><code>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 &lt;- 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 </code></pre>