r - specify different subsets or intervals of a variable in the by argument of data.table -


using following reaction time data (simplified demonstrative purposes):

>dt    subject trialnum blockcode values.trialtype latency correct 1        1        1  practice        cueswitch    3020       1 2        1        1      test           cuerep    4284       1 3        1       21      test        cueswitch    2094       1 4        1       34      test           cuerep    3443       1 5        1       50      test       taskswitch    3313       1 6        2        1  practice        cueswitch    3020       1 7        2        1      test           cuerep    1109       1 8        2       21      test        cueswitch    3470       1 9        2       34      test           cuerep    2753       1 10       2       50      test       taskswitch    3321       1 

i have been using data.table obtain reaction time variables consecutive subsets of trials (specified trialnum, ranges 1 170 in full dataset):

dt1=dt[blockcode=="test" & correct==1, list( rt1=.sd[trialnum>=1 & trialnum<=30 & values.trialtype=="cuerep", mean(latency)], rt2=.sd[trialnum>=31 & trialnum<=60 & values.trialtype=="cuerep", mean(latency)] ), by="subject"] 

the output is

   subject     rt1     rt2 1:       1    4284    3443 2:       2    1109    2753 

however, becomes tedious creating variable each subset when there more 2 or 3 subsets. how can specify subsets more efficiently?

use findinterval or cut subset trialnum column`

an example

# set key use binary search setkey(dt, blockcode,correct,values.trialtype) # subset want dt1 <- dt[.('test',1,'cuerepetition')]  # use cut  define subsets  dt2 <- dt1[,list(latency = mean(latency)),      by=list(subject, trialset = cut(trialnum,seq(0,180,by=30)))] dt2 #    subject trialset latency # 1:       1   (0,30]    4284 # 2:       1  (30,60]    3443 # 3:       2   (0,30]    1109 # 4:       2  (30,60]    2753  #if want separate columns, simple using `dcast` library(reshape2)  dcast(dt2,subject~trialset, value.var = 'latency') #   subject (0,30] (30,60] # 1       1   4284    3443 # 2       2   1109    2753 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -