Last active
January 12, 2021 22:37
-
-
Save luisDVA/24064969714aec8a6978bb1e673224c7 to your computer and use it in GitHub Desktop.
Parsing numbers
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
## %######################################################%## | |
# # | |
#### 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