Mikael Rubin Mikael Rubin - 4 months ago 20
R Question

Re-organizing data to calculate sequence effects

I am trying to use a specific order of a variable in a dataset - valence and - to essentially copy and paste rows into a new dataset. So, for each subID, for every valence = 0, if the next valence = -1 then copy that row to a new dataset called "sequence". The issue is that the trial order was randomized so the sequence is different for each subID (otherwise I'd at least be able to write a big blocky script based on trial number). One important thing I failed to initially mention: There will be a different number of values for each subID, so the number of rows are not even across subID's.

Just as an example of what the data looks like:

SubID location valence reaction_time
1 1 0 500
1 1 -1 600
1 0 1 400
1 0 0 500
2 1 1 700
2 1 0 400
2 0 -1 700
2 0 0 400


I'm pretty much a beginner at this kind of thing (I'm guessing it needs to be a loop?) and don't have a solid starting point, so any help would be hugely appreciated.

Thanks!

Answer

Suppose your data is d:

out <- by(d, d$SubID, function(x) {
    v <- x$valence
    x[which(v[1:(length(v)-1)]==0 & v[2:(length(v))]==-1),]
})
sequence <- do.call(rbind, out)

Result:

> sequence
  SubID location valence reaction_time
1     1        1       0           500
2     2        1       0           400
Comments