tidy datasets: ‘col_types’ to read a variable in the format you want

I have been having issues in my datasets as one variable is both numeric and text. Rather than arguing with excel, tidy offers a quick and painless solution: col_types:

dt <- read_csv("_yourdataset_.csv", col_types = cols(tricky_variable = "c"))[/sourcecode language="r"]

with date, time, numeric, integers, doubles, guess (let tidy guess the type) and skip as some of the available options.

Popupation pyramids updated

Upload the relevant packages and dataset. You can find the data on github here

library(tidyverse)
options(scipen = 9)
setwd("/myworkingdirectory/")
mydt % filter(iso=='UGA')

The dataset includes population estimates at subnational level for Uganda.

# reformat the dataset using tidy

newdf % gather(variable, value,6:761) %>% separate(variable,c('year','sex', 'age'), sep='_') %>% mutate(sex=if_else(sex=='F','female','male')) %>%
spread(year, value) %>%
mutate(age2=recode(age, '1'='0-4', '4'='0-4', '5'='5-9','10'='10-14','15'='15-19', '20'='20-24', '25'= '25-29', '30'='30-34', '35'='35-39', '40'='40-44', '45'='45-49', '50'='50-54', '55'='55-59', '60'='60-64', '65'='65-69', '70'='70-74', '75'='75-79', '80'='80+')) %>%
mutate(age=recode(age, '1'='0', '4'='0'))

newdf$age %
gather(key = year, value = pop, 10:14) %>%
# mutate(pop = pop/1e03) %>%
filter(iso == "UGA"&adm_id==c("UGMIS2014452022"), year %in% c(2000, 2005, 2010, 2015, 2020))

newdf4 %
group_by(iso, adm_id, id, year, sex, age, age2, ageno) %>%
summarise(pop= sum(pop)) %>%
mutate(ageno = ageno + 1)

library(ggthemes)
ggplot(data = newdf4, aes(x = age, y = pop/1000, fill = year)) +
#bars for all but 2100
geom_bar(data = newdf4 %>% filter(sex == "female", year != 2100) %>% arrange(rev(year)),
stat = "identity",
position = "identity", width = 4.5) +
geom_bar(data = newdf4 %>% filter(sex == "male", year != 2100) %>% arrange(rev(year)),
stat = "identity",
position = "identity",
mapping = aes(y = -pop/1000)) +
coord_flip() +
scale_y_continuous(labels = abs, breaks = seq(-600, 600, 250)) +
geom_hline(yintercept = 0) +
theme_economist_white(horizontal = FALSE) +
scale_fill_economist() +
labs(fill = "", x = "", y = "")

Screen Shot 2019-07-14 at 15.46.36

 

A multi-temporal sub-national perspective across low and middle income countries

I will be presenting sub-national fertility maps at session 110 “Projecting fertility in a time of demographic change: will it rise or will it fall?”  this evening in meeting room 311 at 5pm.Screen Shot 2019-04-09 at 15.09.26

Lazy labeling: regular expressions

AKA how start getting around using grep, grepl, sub, gsub, regexp, gregexp.

Main difference between them is that while grep and grepl matches to pattern within the character vector, regexpr and gregexpr return more detail. sub and gsub perform replacement on the basis of a regular expression.

This is my version of lazy labeling: run the same code changing just the input dataset

file <- "BEN_Benin_2013" #myfile
clabel <- substring(file, 1, 3) #country label is the iso3 code
cname <-sub( ".*_(.*)_.*", "\\1", file) #country name
year <- sub( ".*_(.*)_*", "\\1", file) #census year

More to follow on grep and grepl…