Tommaso Guerrini
Tommaso Guerrini

Reputation: 1718

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

Upvotes: 1

Views: 559

Answers (2)

akrun
akrun

Reputation: 886948

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

Upvotes: 3

JWLM
JWLM

Reputation: 442

In a more general way,

paste(strsplit("Tommaso.is.very.stupid", ".", fixed=TRUE)[[1]], collapse=' ')

This is about a factor of two faster than using gsub

microbenchmark(paste(strsplit("Tommaso.is.very.stupid", ".", fixed=TRUE)[[1]], collapse=' '), gsub(".", " ", "Tommaso.is.very.stupid", fixed=FALSE))
Unit: microseconds


    expr
 paste(strsplit("Tommaso.is.very.stupid", ".", fixed = TRUE)[[1]],      collapse = " ")
                                gsub(".", " ", "Tommaso.is.very.stupid", fixed = FALSE)
   min      lq     mean  median      uq    max neval
 4.138  4.7300  5.81864  5.3205  5.7735 29.626   100
 9.352 10.1015 11.05360 10.4000 10.7795 35.177   100

Upvotes: 2

Related Questions