Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Last active January 12, 2021 22:37
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/24064969714aec8a6978bb1e673224c7 to your computer and use it in GitHub Desktop.
Save luisDVA/24064969714aec8a6978bb1e673224c7 to your computer and use it in GitHub Desktop.
Parsing numbers
## %######################################################%##
# #
#### Parsing numbers - your turn ####
# #
## %######################################################%##
# Import the Marine Protected Areas dataset (MPAS-mine.csv)
# Subset to keep only the MPA names and columns with extent data
# Make the columns that hold the MPA extent into usable numeric variables
# Watch out for decimals
# load packages -----------------------------------------------------------
library(readr)
library(dplyr)
library(unheadr)
library(janitor)
library(stringr)
# import data ---------------------------------------------------------------
MPAs <- read_csv("data/MPAS-your.csv") %>% mash_colnames(1)
# subset and parse --------------------------------------------------------
MPAs_ext <- MPAs %>%
select(`Protected Area NAME`, matches("^extent", ignore.case = TRUE)) %>%
remove_empty("rows")
# standardize decimal symbol and parse
MPAs_ext_parsed <- MPAs_ext %>%
mutate(across(starts_with("Extent"), str_replace, ",", ".")) %>%
mutate(across(starts_with("Extent"), parse_number))
# check rounding
print(MPAs_ext_parsed$Extent_sqmi, digits = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment