Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Last active January 12, 2021 22:40
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/000a853d41b34900d2151a5a981964cd to your computer and use it in GitHub Desktop.
Save luisDVA/000a853d41b34900d2151a5a981964cd to your computer and use it in GitHub Desktop.
Whitespace
## %######################################################%##
# #
#### 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