Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Last active January 27, 2021 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisDVA/635ae12054fa36eb93d2407471eb4e64 to your computer and use it in GitHub Desktop.
Save luisDVA/635ae12054fa36eb93d2407471eb4e64 to your computer and use it in GitHub Desktop.
Compound values
## %######################################################%##
# #
#### Compound values - your turn ####
# #
## %######################################################%##
# Import the Marine Protected Areas dataset (MPAS-your.csv)
# Separate the country codes variable (ISO3 and UN scheme)
# Unnest the Reference variable
# > Keep an eye on the separators
# Optional: Arrange the data by ISO3 country code
# load packages -----------------------------------------------------------
library(readr)
library(dplyr)
library(tidyr)
library(unheadr)
library(janitor)
library(stringr)
# import data -------------------------------------------------------------
MPAs <- read_csv("data/MPAS-your.csv") %>%
mash_colnames(1) %>%
clean_names()
# separate columns --------------------------------------------------------
# separate the country codes variable (ISO3 and UN scheme)
MPAs <- MPAs %>% separate(
col = country_code_iso_un,
into = c("country_code_ISO3", "country_code_UN"),
sep = " "
) # two spaces
# separate rows -------------------------------------------------------------
# separate the 'reference' variable
MPAs <- MPAs %>%
separate_rows(reference, sep = "\\|")
# arrange by ISO3 country codes ---------------------------------------------
MPAs <- MPAs %>% arrange(country_code_ISO3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment