Мы можем сначала gather
данные в длинном формате, затем group_by
state
, district
и year
, найти новый ежемесячный rate
, извлечь год из имени столбца и создать list
дат, представляющих последний день месяца за весь год и, наконец, рассчитайте совокупную сумму rate
, чтобы получить добавочное значение для каждого месяца.
library(dplyr)
library(tidyr)
df %>%
gather(key, value, -(1:3)) %>%
group_by(state, district, key) %>%
mutate(rate = (1 + rate * value)^(1/12) - 1,
year = sub(".*(\\d{4})", "\\1", key),
dates = list(seq(as.Date(paste0(year, "-01-01")),
as.Date(paste0(year, "-12-01")), by = "month")- 1)) %>%
unnest() %>%
mutate(rate = cumsum(rate)) %>%
select(-year)
# state district rate key value dates
# <chr> <chr> <dbl> <chr> <dbl> <date>
# 1 AP krishna 1.52 growth_in_2016 0.384 2015-12-31
# 2 AP krishna 3.04 growth_in_2016 0.384 2016-01-31
# 3 AP krishna 4.56 growth_in_2016 0.384 2016-02-29
# 4 AP krishna 6.08 growth_in_2016 0.384 2016-03-31
# 5 AP krishna 7.60 growth_in_2016 0.384 2016-04-30
# 6 AP krishna 9.12 growth_in_2016 0.384 2016-05-31
# 7 AP krishna 10.6 growth_in_2016 0.384 2016-06-30
# 8 AP krishna 12.2 growth_in_2016 0.384 2016-07-31
# 9 AP krishna 13.7 growth_in_2016 0.384 2016-08-31
#10 AP krishna 15.2 growth_in_2016 0.384 2016-09-30
# … with 110 more rows
Данные
df <- structure(list(state = c("AP", "AP"), district = c("krishna",
"guntur"), rate = c(170104.5156, 1343.78134), growth_in_2016 = c(0.3844595,
0.3678), growth_in_2017 = c(0.444595, 0.8445), growth_in_2018 = c(0.323699,
0.36213), growth_in_2019 = c(0.5777, 0.35256), growth_in_2020 = c(0.2669097,
0.9097)), class = c("data.table", "data.frame"), row.names = c(NA, -2L))