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?
Something like
apply(abs( sapply( which(d$event), "-", 1:nrow(d) )),1,min)
will generalize @DWin's answer for more than 2 TRUE
values.
> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With