I'm hoping there is a simple solution to this, but I'm having trouble working it out.
I have population sizes for some cities, like:
df <- data.frame(city = c("A","B","C","D","E","F","G"), start_pop = c(100,200,300,400,500,600,700))
start_pop
is the population in January 2019. Assuming that the population increases by 0.022% each month, I would like to calculate monthly population estimates for each city through 2019, 2020 and 2021, saved as Jan19
, Feb19
etc., which relies on the input of the previous calculation.
I can do it with mutate
, something like:
increase <- 1.00022
df <- data.frame(city = c("A","B","C","D","E","F","G"), start_pop = c(100,200,300,400,500,600,700)) %>%
mutate(Feb19 = start_pop * increase) %>%
mutate(Mar19 = Feb19 * increase) %>%
mutate(Apr19 = Mar19 * increase)
...
But is there a more succinct way of getting the same result, perhaps through a loop or an apply function?