How many days from the list were in given period [R]

Here you can find a tidyverse approch with lubridate and dplyr.

library(lubridate)
library(dplyr)

df %>%
 count(id, start, end, 
       wt = days %within% interval(start, end), 
       name = "number_of_days")

#>      id start      end        number_of_days
#> 1     1 2020-10-05 2020-10-30             11
#> 2     2 2020-10-06 2020-10-29             10
#> 3     3 2020-10-10 2020-10-12              1

For each row, count the number of days within the interval of start and end (extremes included).

(If you don’t want to see start and end just remove them from the first line of count)


Where:

days <- c("2020-10-01",
          "2020-10-03",
          "2020-10-07",
          "2020-10-08",
          "2020-10-09",
          "2020-10-10",
          "2020-10-14",
          "2020-10-17",
          "2020-10-21",
          "2020-10-22",
          "2020-10-27",
          "2020-10-29",
          "2020-10-30")


df <- read.table(text = "  id       start         end
   1    2020-10-05   2020-10-30
   2    2020-10-06   2020-10-29
   3    2020-10-10   2020-10-12", header = TRUE)


days <- as.Date(days)
df$start <- as.Date(df$start)
df$end   <- as.Date(df$end)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top