Last active
January 12, 2021 22:40
-
-
Save luisDVA/000a853d41b34900d2151a5a981964cd to your computer and use it in GitHub Desktop.
Whitespace
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
## %######################################################%## | |
# # | |
#### Whitespace - your turn #### | |
# # | |
## %######################################################%## | |
# - Import the Marine Protected Areas data (MPAS-your.csv) from the previous lesson | |
# - check the Country variable for leading or trailing whitespace | |
# - Remove it if necessary. | |
# load packages ----------------------------------------------------------- | |
library(readr) | |
library(stringr) | |
library(dplyr) | |
# import data ------------------------------------------------------------- | |
MPAs <- read_csv("data/MPAS-your.csv", trim_ws = FALSE) | |
# check for whitespace ---------------------------------------------------- | |
MPAs %>% | |
mutate( | |
leading_ws = str_detect(Country, "^ "), | |
trailing_ws = str_detect(Country, " $") | |
) %>% | |
select(Country, leading_ws, trailing_ws) | |
# remove whitespace ------------------------------------------------------- | |
MPAs <- MPAs %>% mutate(Country = str_squish(Country)) | |
# check if whitespace was removed | |
MPAs %>% | |
mutate( | |
leading_ws = str_detect(Country, "^ "), | |
trailing_ws = str_detect(Country, " $") | |
) %>% | |
select(Country, leading_ws, trailing_ws) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment