мои данные выборки:
structure(list(state = c("Himachal_Pradesh", "Himachal_Pradesh", "Himachal_Pradesh", "Himachal_Pradesh", "Himachal_Pradesh",
"Himachal_Pradesh"), town_names = c("town_a", "town_a", "town_a",
"town_a", "town_a", "town_a"), sales_growth = c(67784.15767,
67784.15767, 67784.15767, 67784.15767, 67784.15767, 67784.15767
), key = c("growth_in_2019", "growth_in_2019", "growth_in_2019",
"growth_in_2019", "growth_in_2019", "growth_in_2019"), value = c(0.0220237807164989,
0.0220237807164989, 0.0220237807164989, 0.0220237807164989, 0.0220237807164989,
0.0220237807164989), year = c("2019", "2019", "2019", "2019",
"2019", "2019"), dates = structure(c(17896, 17927, 17955, 17986,
18016, 18047), class = "Date")), row.names = c(NA, -6L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = c("state", "town1",
"key"), drop = TRUE, indices = list(0:5), group_sizes = 6L, biggest_group_size = 6L, labels = structure(list(
state = "Himachal_Pradesh", town_names = "town_a", key = "growth_in_2019"), row.names = c(NA,
-1L), class = "data.frame", vars = c("state", "town1", "key"
), drop = TRUE))
Я пытаюсь вычислить новые столбцы с именем увеличения Formula = traffic*(1+value)
для первой и второй строк: необходимо получить предыдущее значение строки и затем рассчитать: previous_row*(1+value)
. Это должен быть увеличивающийся заказ.
nv2 <- nv2 %>%
group_by(state, town_names,dates) %>%
mutate(formula = sales_growth*(1+value)) %>%
mutate(formula = formula*(1+value))
state town_names dates formula
Himachal_pradesh town_a 2018-12-31 69,277.02
Himachal_pradesh town_a 2019-01-31 70,802.76
Himachal_pradesh town_a 2019-02-28 72,362.10
Himachal_pradesh town_a 2019-03-31 73,955.79
Himachal_pradesh town_a 2019-04-30 75,584.58