Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a column that indicates the observation's lag from another observation in R?

Tags:

r

time-series

I have dataframe d with a Boolean variable event indicating whether a certain event occurred on a given date. I want to create a new variable that indicates how many observations (days) away the closest event is.

d=structure(list(date = structure(c(-365, -364, -363, -362, -361, 
-360, -359, -358, -357, -356, -355, -354, -353, -352, -351, -350, 
-349, -348, -347, -346), class = "Date"), event = c(TRUE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, 
FALSE)), .Names = c("date", "event"), row.names = c(NA, 20L
), class = "data.frame")

Is there a function that will do this?

like image 745
MattBagg Avatar asked Nov 05 '12 22:11

MattBagg


2 Answers

Something like

apply(abs( sapply( which(d$event), "-", 1:nrow(d) )),1,min)

will generalize @DWin's answer for more than 2 TRUE values.

like image 89
DaveTurek Avatar answered Oct 09 '22 00:10

DaveTurek


> pmin( abs( sapply( which(d$event), "-", 1:nrow(d) )[,1] ) , 
        abs( sapply( which(d$event), "-", 1:nrow(d) )[,2] ) )
 [1] 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 1 2 3 4 5
like image 4
IRTFM Avatar answered Oct 08 '22 22:10

IRTFM