Created
January 25, 2022 17:33
-
-
Save dickoa/d4a56bdd6334b6e3d29da270a5a27c48 to your computer and use it in GitHub Desktop.
popdata demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(popdata) | |
library(unhcrthemes) | |
library(tidyverse) | |
library(janitor) | |
### Most function use "pd_" prefix | |
### "pd" stands for PopData | |
### Log into popdata | |
# pd_login() | |
### | |
demo_asr_2020 <- pd_asr(table = "demographics", | |
year = 2020) | |
glimpse(demo_asr_2020) | |
### How many refugees in Burkina Faso as of December 2020 ? | |
demo_asr_2020 |> | |
filter(asylum == "BKF", populationType == "REF") |> | |
group_by(asylum) |> | |
summarize(ref = sum(total, na.rm = TRUE)) |> | |
ungroup() | |
### How many refugees and IDPs in Burkina Faso as of December 2020 ? | |
demo_asr_2020 |> | |
filter(asylum == "BKF", populationType %in% c("REF", "IDP")) |> | |
group_by(asylum, populationType) |> | |
summarize(val = sum(total, na.rm = TRUE)) |> | |
ungroup() | |
### Wide columns | |
demo_asr_2020 |> | |
filter(asylum == "BKF", populationType %in% c("REF", "IDP")) |> | |
group_by(asylum, populationType) |> | |
summarize(val = sum(total, na.rm = TRUE)) |> | |
ungroup() |> | |
pivot_wider(names_from = populationType, | |
values_from = val) | |
### IDP and REF in Burkina Faso and Nigeria | |
demo_asr_2020 |> | |
filter(asylum %in% c("BKF", "NIG"), | |
populationType %in% c("REF", "IDP")) |> | |
group_by(asylum, populationType) |> | |
summarize(val = sum(total, na.rm = TRUE)) |> | |
ungroup() |> | |
pivot_wider(names_from = populationType, | |
values_from = val) | |
### Do it for the past 3 years | |
map_dfr(2018:2020, function(y) { | |
pd_asr(table = "demographics", year = y) |> | |
mutate(year = y) |> | |
filter(asylum %in% c("BKF", "NIG"), | |
populationType %in% c("REF", "IDP")) |> | |
group_by(year, asylum, populationType) |> | |
summarize(val = sum(total, na.rm = TRUE)) |> | |
ungroup() |> | |
pivot_wider(names_from = populationType, | |
values_from = val) |> | |
arrange(asylum) | |
}) | |
### How many refugees by region | |
demo_asr_2020 <- pd_asr(table = "demographics", | |
year = 2020) | |
demo_asr_2020 <- demo_asr_2020 |> | |
pd_augment(col = "asylum", prefix = "asylum") | |
demo_asr_2020 |> | |
filter(populationType == "REF") |> | |
group_by(asylum_bureau) |> | |
summarise(ref = sum(total, na.rm = TRUE)) |> | |
ungroup() | |
demo_asr_2020 |> | |
filter(is.na(asylum_bureau)) |> | |
View() | |
### Age gender break down | |
demo_asr_2020 |> | |
clean_names() |> | |
select(asylum, origin, population_type, | |
starts_with(c("total_female", "total_male")), | |
-ends_with("total")) |> | |
pivot_longer(cols = contains(c("total_female", "total_male")), | |
names_to = c("sex", "age"), | |
names_pattern = "total_(male|female)_(.*)", | |
values_to = "count") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment