I would appreciate a hint which command to use for the following: I want to compute population estimates for the cities in column "Name" and for every year in column "Year". The column "growth" provides the growth rate. So as formula it would be like:
Population[Lucknow,2030] = Population[Lucknow, 2020] * growth[2030]
and so on. Following df:
df <- data.frame(YEAR=c(2020,2020,2020,2030,2040,2050), NAME=c("Lucknow","Delhi","Hyderadabad",NA,NA,NA), POPULATION=c(3704, 29274,10275,NA,NA,NA), growth=c(1.0,1.0,1.0,1.10,1.18,1.24))
Year Name Population growth
2020 Lucknow 3704 1.0000000
2020 Delhi 29274 1.0000000
2020 Hyderabad 10275 1.0000000
2030 <NA> NA <NA> 1.10
2040 <NA> NA <NA> 1.18
2050 <NA> NA <NA> 1.24
edit: Following what Dom (thank you!) wrote below, the input would be:
df <- tibble( year = rep(c(2020,2030,2040,2050), each = 3), city =rep(c("Lucknow","Delhi","Hyderadabad"), times = 4), pop = c(3704, 29274,10275, rep(NA_integer_, times = 9)), growth = rep(c(1.0, 1.10, 1.18, 1.24), each = 3) )
year city pop growth
<dbl> <chr> <dbl> <dbl>
1 2020 Lucknow 3704 1
2 2020 Delhi 29274 1
3 2020 Hyderadabad 10275 1
4 2030 Lucknow NA 1.1
5 2030 Delhi NA 1.1
6 2030 Hyderadabad NA 1.1
7 2040 Lucknow NA 1.18
8 2040 Delhi NA 1.18
9 2040 Hyderadabad NA 1.18
10 2050 Lucknow NA 1.24
11 2050 Delhi NA 1.24
12 2050 Hyderadabad NA 1.24
The output should look like:
Year Name Population growth
2020 Lucknow 3704 1.0000000
2020 Delhi 29274 1.0000000
2020 Hyderabad 10275 1.0000000
2030 Lucknow 4074.4 1.1000000
2030 Delhi 32201.4 1.1000000
2030 Hyderabad 11302.5 1.1000000
....
How to fill the NAs in the tibble?
I had various attempts with merge and dplyr::mutate, but failed to identify what I need to do here given that this is a vector operation. I'd be happy for any leads towards the correct command to do such a basic operation.
Thanks!