Tommaso Guerrini Tommaso Guerrini - 5 days ago 6
R Question

String Manipulation in R, splitting and collapsing with a different separator

I have a question about splitting strings in R.

I have a string like:

"Tommaso.is.very.stupid"


and I would like to have:

"Tommaso is very stupid"


Is there a straightforward way to do so?

strsplit("Tommaso.is.very.stupid","\\.")


gets me a list with one vector composed of the 4 words separated, how do I collapse them back together with a space between them?

Thank you and sorry for bothering with such a small issue

Answer

We can use gsub to replace the . with space " "

gsub(".", " ", "Tommaso.is.very.stupid", fixed = TRUE)
#[1] "Tommaso is very stupid"

Benchmarks

library(microbenchmark)
library(stringi)
set.seed(24)
v1 <- stri_rand_strings(1e6, 100, pattern = "[A-F.]")

f1 <- function() gsub(".", " ", v1, fixed = TRUE)      
f2 <- function() paste(strsplit(v1, ".", fixed=TRUE)[[1]], collapse=' ')
microbenchmark(f1(), f2(), times = 20L, unit = "relative")
#Unit: relative
# expr      min       lq     mean  median      uq      max neval
#f1() 1.000000 1.000000 1.000000 1.00000 1.00000 1.000000    20
#f2() 2.575039 4.501027 3.074894 4.80972 2.87893 1.745782    20
Comments