Hi All,
I've been trying to compare if the previous value in a variable is equal to
a binary value..(i.e i want to check if the last event was a yes or no)
i've been trying to write some code for this, but it seems overly elaborate,
can anyone suggest a better / shorter / neater way?
The below doesn't quite work but shows my idea of splitting by the factor
id, then creating a new vector that is lead, then i was going to use an
ifelse clause..
But as i suggested this seem very elaborate.. my sample code below
DF <- read.table(textConnection("timestamp; id
2008-05-27 22:57:00; 763830873067
2008-05-27 23:00:00; 763830873067
2008-05-27 23:01:00; 763830873067
2008-05-27 23:01:00; 763830873067
2008-06-05 11:34:00; 763830873067
2008-05-29 23:08:00; 765253440317
2008-05-29 23:06:00; 765253440317
2008-05-29 22:52:00; 765253440317
2008-05-29 22:52:00; 765253440317
2008-05-29 23:04:00; 765253440317
2008-06-27 19:34:00; 765253440317
2008-07-09 15:45:00; 765329002557
2008-07-06 19:24:00; 765329002557
2008-07-09 15:46:00; 765329002557
2008-07-07 13:05:00; 765329002557
2008-05-16 22:40:00; 765329002557
2008-06-08 11:24:00; 765329002557
2008-06-08 12:33:00; 765329002557"),as.is
=TRUE,sep=";",strip.white=TRUE,header=TRUE)
closeAllConnections()
DF$time <- ifelse(DF$timestamp > as.POSIXct("2008-07-01"), 1, 0)
last_event <- lapply(split(test, test$ID), function(.df){
lead_func_temp <- c(NA,.df$TIME [ - length(.df$TIME)])
temp <-
data.frame(ID=as.character(.df$ID),TIME=.df$TIME,
DIFF=rep(lead_func_temp,nrow(.df)))
return(temp)
})
DF$last_event <- do.call(rbind, last_event)
Mike
[[alternative HTML version deleted]]