I'd like to calculate time intervals for subsets of a data frame. Here's some example data:
dates
Model start test1 test2
1 2010-09-01 2014-09-09 2019-09-09
2 2010-06-01 2014-06-06 2019-06-06
aka
dates <- structure(list(Model = 1:2, start = c("2010-09-01", "2010-06-01"), test1 = c("2014-09-09", "2014-06-06"), test2 = c("2019-09-09",
"2019-06-06")), row.names = c(NA, -2L), class = "data.frame")
Say I want to calculate the interval for Model 1 as the time from start
to test1
. I call the new variable elapsed
. I tried:
library(lubridate)
dates$start <- as_date(dates$start)
dates$test1 <- as_date(dates$test1)
dates$elapsed[dates$Model==1] <- dates$start %--% dates$test1
I get this warning message:
In dates$elapsed[dates$Model == 1] <- dates$start %--% dates$test1 : number of items to replace is not a multiple of replacement length
The result looks OK though.
dates$elapsed <- round(as.duration(dates$elapsed) / dyears(1),digits=2)
dates$elapsed
[1] 4.02 NA
I found a bit of explanation here Getting an error "number of items to replace is not a multiple of replacement length" but I'm still not sure how to avoid the warning. Thanks.