Мы можем использовать map2
для создания последовательности 'Date' между столбцами 'BEGIN', 'END', затем unnest
для расширения набора данных
library(dplyr)
library(tidyr)
library(purrr)
library(lubridate)
df1 %>%
mutate_at(vars(starts_with('CONTRACT')), ymd) %>%
mutate(DATE = map2(CONTRACT_BEGIN, CONTRACT_END, seq, by = 'day')) %>%
pivot_wider(names_from = CURVE_DATE, values_from = FP) %>%
select(-c(CONTRACT_BEGIN, CONTRACT_END)) %>%
unnest(c(DATE))
# A tibble: 29 x 5
# REGION BASIS_LOCATION DATE `2015-12-30` `2016-01-01`
# <chr> <chr> <date> <int> <int>
# 1 x y 2016-02-01 2 3
# 2 x y 2016-02-02 2 3
# 3 x y 2016-02-03 2 3
# 4 x y 2016-02-04 2 3
# 5 x y 2016-02-05 2 3
# 6 x y 2016-02-06 2 3
# 7 x y 2016-02-07 2 3
# 8 x y 2016-02-08 2 3
# 9 x y 2016-02-09 2 3
#10 x y 2016-02-10 2 3
# … with 19 more rows
data
df1 <- structure(list(REGION = c("x", "x"), BASIS_LOCATION = c("y",
"y"), CURVE_DATE = c("2015-12-30", "2016-01-01"),
CONTRACT_BEGIN = c("2016-02-01",
"2016-02-01"), CONTRACT_END = c("2016-02-29", "2016-02-29"),
FP = 2:3), class = "data.frame", row.names = c(NA, -2L))