Home > Software engineering >  How to group dates by periods of n days in R?
How to group dates by periods of n days in R?

Time:10-30

Input
dates <- as.Date(c("2022-01-20", "2022-01-30", "2022-01-31", "2022-02-10"))
n     <- 10 # number of days for each period
Output
group <- c(1, 2, 2, 3)

Group 1 corresponds to the earliest date and dates that are in a period of n - 1 days at most after the earliest date of the vector.

One could convert each date to its number of days (since when 1900?) and use the %/% operator but I want a more proper solution.

Another solution would be to use seq(min(dates), max(dates), by = "10 days") but I can't manage to find such a way to achieve the output.

CodePudding user response:

Like this?

dates <- as.Date(c("2022-01-20", "2022-01-30", "2022-01-31", "2022-02-10"))
n     <- 10 # number of days for each period

as.integer(factor(as.integer(dates) %/% n))
#> [1] 1 2 2 3

Created on 2022-10-30 with reprex v2.0.2

Or with findInterval.

findInterval(dates, seq(min(dates), max(dates), by = "10 days"))
#> [1] 1 2 2 3

Created on 2022-10-30 with reprex v2.0.2

  • Related