Created
October 22, 2021 03:00
-
-
Save MonkmanMH/5910d485e1e869b814fe1611383c9505 to your computer and use it in GitHub Desktop.
use `separate()` function from {tidyr} to split a variable
This file contains 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
# OBJECTIVE: | |
# * latitude and longitude stored with degrees with degree symbol and minutes with decimal | |
# * separate into two columns for each, with degrees and minutes separated | |
# step 1 - create a test tibble called "geo_loc" | |
geo_loc <- tribble( ~ latitude, ~ longitude, | |
"48º 4.206", "124º 45.553", | |
"46º 59.4942", "124º 12.6362") | |
# --- | |
# step 2 - split into degrees and minutes columns | |
# use the `separate()` function from {tidyr} https://tidyr.tidyverse.org/reference/separate.html | |
# note that the "sep =" argument has the degree symbol and the space | |
geo_loc %>% | |
tidyr::separate(latitude, into = c("lat_deg", "lat_min"), sep = "º ") %>% | |
# same function, but note the `remove =` argument to keep the source column | |
tidyr::separate(longitude, into = c("lon_deg", "lon_min"), sep = "º ", remove = FALSE) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment