Используя tidyverse
, мы можем создать последовательность между Start
и End
для каждого Person
и count
числа Date
.
library(tidyverse)
df %>%
mutate_at(-1,lubridate::mdy) %>%
mutate(Date = map2(Start, End, seq, by = "1 day")) %>%
unnest(Date) %>%
count(Date)
# A tibble: 1,096 x 2
# Date n
# <date> <int>
# 1 1990-01-01 1
# 2 1990-01-02 1
# 3 1990-01-03 1
# 4 1990-01-04 1
# 5 1990-01-05 1
# 6 1990-01-06 1
# 7 1990-01-07 1
# 8 1990-01-08 1
# 9 1990-01-09 1
#10 1990-01-10 1
# … with 1,086 more rows
данных
df <- structure(list(Person = structure(2:1, .Label = c("Eve", "John"
), class = "factor"), Start = structure(1:2, .Label = c("01/01/1990",
"12/27/1991"), class = "factor"), End = structure(1:2, .Label = c("01/07/1992",
"12/31/1992"), class = "factor")), class = "data.frame", row.names = c(NA, -2L))