Skip to content

Instantly share code, notes, and snippets.

@SimonCoulombe
Created June 22, 2021 02:35
Show Gist options
  • Save SimonCoulombe/258211e0a2703657bb2bb3347c85706a to your computer and use it in GitHub Desktop.
Save SimonCoulombe/258211e0a2703657bb2bb3347c85706a to your computer and use it in GitHub Desktop.
library(tidyverse)
library(cancensus)
library(sf)
library(stringr)
library(mapview)
#options(cancensus.api_key = "your_api_key")
#options(cancensus.cache_path = "custom cache path")
list_census_regions('CA16') %>%
filter(level == "CMA", name %in% c("Vancouver","Toronto"))
census_data <- get_census(dataset='CA16',
regions=list(PR=c("24")),
level='DB',
geo_format = "sf",
use_cache = TRUE)
mydata <- census_data %>%
mutate(
area = st_area(geometry),
densite = Population * 1e6 / as.numeric(area )
)
list_census_regions('CA16') %>%
filter(level == "CMA" ) %>%
filter(str_detect(toupper(name), "QUÉBEC") )
# region name level pop municipal_status CMA_UID CD_UID PR_UID
# <chr> <chr> <chr> <int> <chr> <chr> <chr> <chr>
# 1 24421 Québec CMA 800296 B NA NA 24
mydata %>%
filter(CMA_UID == 24421) %>%
mapview(zcol = "densite")
write_rds(mydata, "mydata.rds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment