Created
October 24, 2019 16:26
-
-
Save k5cents/eca8c10c89c95805740d6af08a614bcf to your computer and use it in GitHub Desktop.
Trying to separate() a tibble right-to-left
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
library(tibble) | |
library(tidyr) | |
library(glue) | |
data <- tribble( | |
~ address, | |
"8548 Mammoth Drive, Sumter, SC, 29150", | |
"9725 Spring St., Apt 1, Piscataway, NJ, 08854", | |
"Apple co., 75 Applegate Ave., Encino, CA, 91316", | |
"26 Sherwood Ave., Reading, MA, 01867", | |
"413 Cactus Drive, Bldg 5, Waxhaw, NC, 28173", | |
"12 Queen Rd., Spring Hill, FL, 34608", | |
"John Smith, 342 New St., Suite 120, South Richmond Hill, NY, 11419", | |
"17 Purple Finch Ave., Aberdeen, SD, 57401", | |
"75 Court Circle, Brookfield, WI, 53045", | |
"559 Sherman Court, Fort Wayne, IN, 46804" | |
) | |
# typical default separate | |
data %>% | |
separate( | |
col = address, | |
sep = ",\\s", | |
into = c("address1", "address2", "city", "state", "zip") | |
) | |
# fill left kind of works | |
data %>% | |
separate( | |
col = address, | |
sep = ",\\s", | |
into = c("address1", "address2", "city", "state", "zip"), | |
fill = "left" | |
) | |
# create many left cols and unite | |
data %>% | |
separate( | |
col = address, | |
sep = ",\\s", | |
into = c(glue("address{1:10}"), "city", "state", "zip"), | |
fill = "left" | |
) %>% | |
unite( | |
starts_with("address"), | |
col = address, | |
sep = " ", | |
na.rm = TRUE | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment