You can try rbind
in do.call
. The use of [
in lapply
is done to get 3 results in case the city is missing.
x <- c("Africa / Kenya / Nairobi", "Africa/Kenya/Nairobi", "Africa / Kenya")
y <- do.call(rbind, lapply(strsplit(x, "/", TRUE), "[", 1:3))
y <- trimws(y, whitespace = " ")
y
# [,1] [,2] [,3]
#[1,] "Africa" "Kenya" "Nairobi"
#[2,] "Africa" "Kenya" "Nairobi"
#[3,] "Africa" "Kenya" NA
Or using data.table
:
x <- c("Africa / Kenya / Nairobi", "Africa/Kenya/Nairobi", "Africa / Kenya")
y <- do.call(cbind, data.table::tstrsplit(x, "/", TRUE))
y <- trimws(y, whitespace = " ")
y
# [,1] [,2] [,3]
#[1,] "Africa" "Kenya" "Nairobi"
#[2,] "Africa" "Kenya" "Nairobi"
#[3,] "Africa" "Kenya" NA
Benchmark
#x <- rep("Africa / Kenya / Nairobi", 1000000) #Timings will depend on the used dataset
n <- 1e6L
f1 <- function(n) replicate(n, paste(sample(letters, sample(5:15, 1), TRUE), collapse = ""))
f2 <- function(n) sample(c("/", " /", "/ ", " / "), n, TRUE)
set.seed(42)
x <- paste0(f1(n), f2(n), f1(n), sample(c(paste0(f2(n%/%2L), f1(n%/%2L)), rep("", n - n%/%2L))))
system.time( #Method given in the question
sapply(x, function(loc) trimws(strsplit(loc,"/", fixed = TRUE)[[1]][2])))
# User System verstrichen
# 47.718 0.004 47.798
system.time( #Using strsplit and trimws
trimws(do.call(rbind, lapply(strsplit(x, "/", TRUE), "[", 1:3)), whitespace = " "))
# User System verstrichen
# 5.446 0.008 5.454
system.time( #Using data.table::tstrsplit and trimws
trimws(do.call(cbind, data.table::tstrsplit(x, "/", TRUE)), whitespace = " "))
# User System verstrichen
# 2.365 0.012 2.376
system.time( #Using readr::read_delim from @Anoushiravan R
readr::read_delim(x, delim = "/", quote = "", trim_ws = TRUE, col_names = FALSE))
# User System verstrichen
# 1.961 0.024 2.222
system.time( #Using data.table::tstrsplit with " */ *"
do.call(cbind, data.table::tstrsplit(x, " */ *", perl=TRUE)))
# User System verstrichen
# 1.394 0.000 1.394
system.time( #Using read.table from @akrun
read.table(text = x, sep = "/", header = FALSE, fill = TRUE, strip.white = TRUE, na.strings = ""))
# User System verstrichen
# 1.298 0.004 1.302
system.time( #Using data.table::fread from @akrun
data.table::fread(text = paste(x, collapse="\n"), sep="/", fill = TRUE, na.strings = ""))
# User System verstrichen
# 1.146 0.016 0.996
system.time( #Using read.table with additional argiments
read.table(text = x, sep = "/", header = FALSE, fill = TRUE, strip.white = TRUE, na.strings = "", nrows=length(x), comment.char = "", colClasses = c("character")))
# User System verstrichen
# 1.076 0.000 1.076
system.time( #Using data.table::fread with stringr::str_c (or stringi::stri_c)
data.table::fread(text = stringr::str_c(x, collapse="\n"), sep="/", fill = TRUE, na.strings = ""))
# User System verstrichen
# 0.780 0.000 0.624
Using data.table::fread
and creating the input string with stringr::str_c
looks like to be currently the fastest of the given methods.