I'm trying to group by the column "Subregion" to get the population number for each Subregion in the latest and earliest years (1950, 2020) found in the dataset, and it doesn't return a grouped dataset for some reason. I tried to remove and reorder some of the code lines and nothing works
first_year <- min(as.numeric(data.tidy$Year))
last_year <- max(as.numeric(data.tidy$Year))
Population.Growth.Subregion <- data.tidy %>%
filter(Year %in% c(first_year, last_year), Population.Average %in% "other") %>%
na.omit() %>%
spread(Year, Population.Total) %>%
group_by(Subregion) %>%
mutate(Growth = 100*(
(get(as.character(last_year))/get(as.character(first_year)))^
(1/(last_year-first_year)) - 1)
) %>%
print()
Returns
Country Subregion Code Age.Group Population.Average `1950` `2020` Growth
<chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 Algeria Northern Africa DZA 15_24 other 1724431 5910182 1.78
2 Algeria Northern Africa DZA 25_64 other 3230562 21485130 2.74
3 Algeria Northern Africa DZA 5_14 other 2199620 8457374 1.94
4 Algeria Northern Africa DZA 65_or_over other 314503 2956839 3.25
5 Algeria Northern Africa DZA Under_5 other 1403134 5041518 1.84
6 Angola Middle Africa AGO 15_24 other 884289 6415084 2.87
7 Angola Middle Africa AGO 25_64 other 1705016 10482505 2.63
8 Angola Middle Africa AGO 5_14 other 1085648 9453425 3.14
9 Angola Middle Africa AGO 65_or_over other 133832 720250 2.43
10 Angola Middle Africa AGO Under_5 other 739236 5795004 2.99
# … with 255 more rows
Edit
That's how the dataset looks like after the snippet I added above:
That's how it looked beforehand
That's what I wanted to get