TJ87
TJ87

Reputation: 542

How to calculate time interval for a subset?

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.

Upvotes: 1

Views: 80

Answers (2)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6759

Replacing your last line with this:

library(tidyverse)
dates %>% mutate(elapsed = ifelse(Model==1, start %--% test1, NA))

will avoid the warning, and get the same result.

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 388797

The warning makes complete sense since number of items to replace (dates$elapsed[dates$Model==1]) is of length 1 whereas replacement length (dates$start %--% dates$test1) is of length 2.

length(dates$elapsed[dates$Model==1])
#[1] 1

length(dates$start %--% dates$test1)
#[1] 2

You need to subset (dates$Model==1) from both the sides

library(lubridate)
dates$elapsed[dates$Model==1] <- dates$start[dates$Model==1] %--% dates$test1[dates$Model==1]

#dates
#  Model      start      test1      test2   elapsed
#1     1 2010-09-01 2014-09-09 2019-09-09 126921600
#2     2 2010-06-01 2014-06-06 2019-06-06        NA

Upvotes: 0

Related Questions