Skip to content

Commit

Permalink
add 3 letter country code
Browse files Browse the repository at this point in the history
  • Loading branch information
hturner committed Nov 18, 2019
1 parent 675da16 commit 840dbb2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 16 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Depends: R (>= 2.10)
RoxygenNote: 6.1.1
Suggests:
dplyr,
knitr,
gganimate,
ggplot2,
ggthemes,
Expand Down
5 changes: 3 additions & 2 deletions R/tdor.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#' \item{Age}{Age (character), typically age in years, but may also be
#' a range (if reports are inconsistent), or an approximation (e.g.
#' "Approx. 30" or "Under 35").}
#' \item{Age_min}{Minium age (numeric) if this can be defined from Age.}
#' \item{Age_max}{Maxium age (numeric) if this can be defined from Age.}
#' \item{Age min}{Minium age (numeric) if this can be defined from Age.}
#' \item{Age max}{Maxium age (numeric) if this can be defined from Age.}
#' \item{Photo source}{URL where photo was obtained from for
#' tdor.translivesmatter.info website.}
#' \item{Date}{Date of death.}
Expand All @@ -46,6 +46,7 @@
#' \item{State/Province}{The state or province. If `NA`, the state/province
#' is part of `Location`.}
#' \item{Country}{Country where death occurred.}
#' \item{Country code}{ISO-3 country code (three letter code).}
#' \item{Latitude}{Latitude of location.}
#' \item{Longitude}{Longitude of location.}
#' \item{Cause of death}{Category of cause of death.}
Expand Down
19 changes: 14 additions & 5 deletions data-raw/tdor.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
library("countrycode")
library("dplyr")
library("janitor")
library("lubridate")
library("purrr")
library("readr")
library("tidyr")
library("usethis")
library("WDI")

# Get Data ----
# Data sourced from https://bitbucket.org/annajayne/tdor_data/downloads/, unzipped and the contents of the data folder placed in data-raw
Expand Down Expand Up @@ -68,12 +70,12 @@ tdor <- files %>%

tdor <- tdor %>%
# sometimes age is a range; separate into min and max
separate(Age, into = c("Age_min", "Age_max"), sep = "-", remove = FALSE) %>%
mutate(Age_min = suppressWarnings(ifelse(!is.na(as.numeric(Age_min)),
as.numeric(Age_min),
separate(Age, into = c("Age min", "Age max"), sep = "-", remove = FALSE) %>%
mutate(`Age min` = suppressWarnings(ifelse(!is.na(as.numeric(`Age min`)),
as.numeric(`Age min`),
as.numeric(sub("(Approx. )",
"", Age)))),
Age_max = suppressWarnings(ifelse(!is.na(Age_max), as.numeric(Age_max),
`Age max` = suppressWarnings(ifelse(!is.na(`Age max`), as.numeric(`Age max`),
as.numeric(sub("(Approx. |Under )",
"", Age))))) %>%
# simple conversion to TDOR period no longer works
Expand All @@ -97,7 +99,14 @@ tdor <- tdor %>%
# 1st October to 30th September from now on
TRUE ~ ifelse(Month %in% 1:9, Year, Year + 1)
)) %>%
select(Name:Date, Month, Year, TDoR, everything(), -Photo)
# country code
mutate(`Country code` = countrycode(Country, "country.name", "iso3c")) %>%
# logical order
select(Name, Age, `Age min`, `Age max`,
Date, Month, Year, TDoR,
Location, `State/Province`, Country, `Country code`, Latitude, Longitude,
`Cause of death`, Description,
`Photo source`, `Source ref`, Tweet, Permalink)

use_data(tdor, overwrite = TRUE)

Expand Down
Binary file modified data/tdor.rda
Binary file not shown.
5 changes: 3 additions & 2 deletions man/tdor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vignettes/exploring_2019_data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Load required packages.
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
library(maps)
library(tdor)
library(lubridate)
```

## Some code and graphs to explore the 2019 data
Expand Down Expand Up @@ -58,7 +58,7 @@ Where we have ages... what were those ages?
```{r deaths_by_age, warning=FALSE}
tdor_subset %>%
ggplot(aes(x = (Age_min + Age_max)/2)) +
ggplot(aes(x = (`Age min` + `Age max`)/2)) +
geom_bar() +
ggtitle("Deaths by age") +
labs(y = "Deaths", x = "Age at death",
Expand Down
8 changes: 4 additions & 4 deletions vignettes/exploring_data_set.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ In some cases there is ambiguity over the age, so we have a minimum and a maximu

```{r deaths_by_age, warning=FALSE}
tdor %>%
filter(Age_min > 0 & Age_max > 0) %>%
ggplot(aes(x = (Age_min + Age_max)/2)) +
filter(`Age min` > 0 & `Age max` > 0) %>%
ggplot(aes(x = (`Age min` + `Age max`)/2)) +
geom_bar() +
ggtitle("Deaths by age") +
labs(y = "Deaths")
tdor %>%
filter(Age_min > 0 & Age_max > 0) %>%
ggplot(aes(x = (Age_min + Age_max)/2)) +
filter(`Age min` > 0 & `Age max` > 0) %>%
ggplot(aes(x = (`Age min` + `Age max`)/2)) +
geom_histogram(binwidth = 5) +
ggtitle("Deaths by age") +
labs(y = "Deaths")
Expand Down

0 comments on commit 840dbb2

Please sign in to comment.