Juul - 3 months ago 14
R Question

# Calculate post error slowing in R

For my research, I would like to calculate the post-error slowing in the stop signal task to find out whether people become slower after they failed to inhibit their response. Here is some data and I would like to do the following:

1. For each subject determine first if it was a stop-trial (signal = 1)

2. For each stop-trial, determine if it is correct (signal = 1 & correct = 2) and then determine whether the next trial (thus the trial directly after the stop-trial) is a go-trial (signal = 0)

• Then calculate the average reaction time for all these go-trials that directly follow a stop trial when the response is correct (signal = 0 & correct = 2).

3. For each incorrect stop trial (signal = 1 & correct = 0) determine whether the next trial (thus the trial directly after the stop-trial) is a go-trial (signal = 0)

• Then calculate the average reaction time for all these go-trials that directly follow a stop-trial when the response is correct (correct = 2).

4. Then calculate the difference between the RTs calculated in step 2 and 3 (= post-error slowing).

I'm not that experienced in R to achieve this. I hope someone can help me with this script.

``````    subject trial   signal  correct RT
1   1   0   2   755
1   2   0   2   543
1   3   1   0   616
1   4   0   2   804
1   5   0   2   594
1   6   0   2   705
1   7   1   2   0
1   8   1   2   0
1   9   0   2   555
1   10  1   0   604
1   11  0   2   824
1   12  0   2   647
1   13  0   2   625
1   14  0   2   657
1   15  1   0   578
1   16  0   2   810
1   17  1   2   0
1   18  0   2   646
1   19  0   2   574
1   20  0   2   748
1   21  0   0   856
1   22  0   2   679
1   23  0   2   738
1   24  0   2   620
1   25  0   2   715
1   26  1   2   0
1   27  0   2   675
1   28  0   2   560
1   29  1   0   584
1   30  0   2   564
1   31  0   2   994
1   32  1   2   0
1   33  0   2   715
1   34  0   2   644
1   35  0   2   545
1   36  0   2   528
1   37  1   2   0
1   38  0   2   636
1   39  0   2   684
1   40  1   2   0
1   41  0   2   653
1   42  0   2   766
1   43  0   2   747
1   44  0   2   821
1   45  0   2   612
1   46  0   2   624
1   47  0   2   665
1   48  1   2   0
1   49  0   2   594
1   50  0   2   665
1   51  1   0   658
1   52  0   2   800
1   53  1   2   0
1   54  1   0   738
1   55  0   2   831
1   56  0   2   815
1   57  0   2   776
1   58  0   2   710
1   59  0   2   842
1   60  1   0   516
1   61  0   2   758
1   62  1   2   0
1   63  0   2   628
1   64  0   2   713
1   65  0   2   835
1   66  1   0   791
1   67  0   2   871
1   68  0   2   816
1   69  0   2   769
1   70  0   2   930
1   71  0   2   676
1   72  0   2   868
2   1   0   2   697
2   2   0   2   689
2   3   0   2   584
2   4   1   0   788
2   5   0   2   448
2   6   0   2   564
2   7   0   2   587
2   8   1   0   553
2   9   0   2   706
2   10  0   2   442
2   11  1   0   245
2   12  0   2   601
2   13  0   2   774
2   14  1   0   579
2   15  0   2   652
2   16  0   2   556
2   17  0   2   963
2   18  0   2   725
2   19  0   2   751
2   20  0   2   709
2   21  0   2   741
2   22  1   0   613
2   23  0   2   781
2   24  1   2   0
2   25  0   2   634
2   26  1   2   0
2   27  0   2   487
2   28  1   2   0
2   29  0   2   692
2   30  0   2   745
2   31  1   2   0
2   32  0   2   610
2   33  0   2   836
2   34  1   0   710
2   35  0   2   757
2   36  0   2   781
2   37  0   2   1029
2   38  0   2   832
2   39  1   0   626
2   40  1   2   0
2   41  0   2   844
2   42  0   2   837
2   43  0   2   792
2   44  0   2   789
2   45  0   2   783
2   46  0   0   0
2   47  0   0   468
2   48  0   2   686
``````

Does that help you? A little bit redundant maybe, but I tried to follow your steps as best as possible (not sure whether I mixed something up, please check for yourself looking at the table). The idea is to put the data in a csv file first and treat it as a data frame. Find the csv raw file here: http://pastebin.com/X5b2ysmQ

``````data <- read.csv("datatable.csv",header=T)

data[,"condition1"] <- data[,"signal"] == 1
data[,"condition2"] <- data[,"condition1"] & data[,"correct"] == 2

data[,"RT1"] <- NA
for(i in which(data[,"condition2"])){
if( nrow(data)>i && !data[i+1,"condition1"] && data[i+1,"correct"] == 2  )
# next is a go trial
data[i+1,"RT1"] <- data[i+1,"RT"]
}
averageRT1 <- mean( data[ !is.na(data[,"RT1"]) ,"RT1"] )

data[,"RT2"] <- NA
for(i in which(data[,"condition1"] & data[,"correct"] == 0)){
if( nrow(data)>i && !data[i+1,"condition1"] && data[i+1,"correct"] == 2  )
# next is a go trial
data[i+1,"RT2"] <- data[i+1,"RT"]
}
averageRT2 <- mean( data[ !is.na(data[,"RT2"]) ,"RT2"] )

postErrorSlowing <- abs(averageRT2-averageRT1)
``````
Source (Stackoverflow)