Given:
test <- data.frame(Participant= c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3),
Day = c(0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9),
Value= c(1:30))
I want to arrive at:
test <- data.frame(Participant= c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3),
Day = c(0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9),
Value= c(1:30),
LaggedValue= c("NA", 1,2,3,4,5,6,7,8,9, "NA", 11,12,13,14,15,16,17,18,19, "NA", 21,22,23,24,25,26,27,28,29))
I have tried the following which allows me to time lag the variable but does so through the entire column. I'd like to time lag based on the ParticipantID or Day variable such that the time lag returns an "NA" when it encounters a new participant number or Day=0:
test$LaggedValue <- c(NA, test$Value[seq_along(test$Value) -1])
I'm not sure how I can add an "if" statement or base it on the Participant/Day variable. Would a nest() function possibly work here?
To split a group variable,
dplyr
library (or theby
command) are what you need, something like the following (I don't have access to an R interpreter right now):This paradigm is the very-well-known
split-apply-combine
. Don't try to hack it up with if statements.EDIT: or
data.table
package, as per Gary's answer