Skip to content

Instantly share code, notes, and snippets.

@statgeek
Created July 10, 2019 03:35
Show Gist options
  • Select an option

  • Save statgeek/243460c7b3d331f21af094d06e53f8ef to your computer and use it in GitHub Desktop.

Select an option

Save statgeek/243460c7b3d331f21af094d06e53f8ef to your computer and use it in GitHub Desktop.
R - calculate percent of population over 60 by Province
install.packages("cansim")
install.packages("tidyverse")
library("cansim")
library("tidyverse")
pop <- get_cansim("17-10-0005-01")
#keep only relevant data
pop1 <- pop %>%
filter(Sex == "Both sexes",
`Age group` %in%
c("All ages", "65 years and over",
"60 to 64 years")) %>%
rename(Age_Group = `Age group`, Value=VALUE, Year = REF_DATE) %>%
select (-c("DGUID", "UOM", "UOM_ID", "SCALAR_FACTOR", "SCALAR_ID", "VECTOR", "COORDINATE", "STATUS")) %>%
select (Year, GEO, Value, Age_Group)
pop2 <- pop1 %>%
spread(Age_Group, Value) %>%
mutate(pct_60_over = (`60 to 64 years` +`65 years and over`)/ `All ages`,
pct_65_over = `65 years and over`/ `All ages`,
Year = as.numeric(Year))
ggplot(data = pop2) +
geom_line(mapping = aes(x = Year, y = pct_60_over)) +
facet_wrap(~ GEO, nrow = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment