title: "SCB example" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Vignette Title} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8}


SCB example

Building query

library(scbapi)
r <- scb_api("ssd")
r
scb_api("ssd/BE")
scb_api("ssd/BE/BE0101")
scb_api("ssd/BE/BE0101/BE0101A")

Retrive data

r <- scb_api("ssd/BE/BE0101/BE0101A/FolkmangdTatortH")
res <- r$get()

Inspect

head(res)

Format output

library(dplyr)
library(tidyr)
library(stringr)

dd <- res %>% 
  gather(year, pop, Population.1960:Population.1980) %>% 
  mutate(
    year = str_extract(year, "[0-9]{4}") %>% as.integer(),
    county = str_extract(region, "[\\(\\) A-ZÅÄÖa-zåäö\\*]*$") %>% str_replace_all("[\\(\\)]", ""),
    pop = as.integer(pop)
  ) %>% tbl_df()
dd

ddd <- dd %>% 
  group_by(county, year) %>% 
  summarise(pop = sum(pop, na.rm = TRUE))

Visualise

library(ggplot2)

ids <- sample(unique(ddd$county[ddd$year == 1965]), 4)
d <- ddd %>% filter(county %in% ids)
ggplot(d, aes(year, pop, color = county, group = county)) + geom_line() + 
  theme(legend.position = "bottom") + 
  theme_bw()


junkka/scbapi documentation built on May 20, 2019, 4:24 a.m.