Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Created October 22, 2021 03:00
Show Gist options
  • Save MonkmanMH/5910d485e1e869b814fe1611383c9505 to your computer and use it in GitHub Desktop.
Save MonkmanMH/5910d485e1e869b814fe1611383c9505 to your computer and use it in GitHub Desktop.
use `separate()` function from {tidyr} to split a variable
# 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