13

I have:

"word1.word2"

and I want:

"word1" "word2"

I know I have to use strsplit with perl=TRUE, but I can't find the regular expression for a period (to feed to the split argument).

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
Antoine
  • 1,649
  • 4
  • 23
  • 50

2 Answers2

17

There are several ways to do this, both with base R and with the common string processing packages (like "stringr" and "stringi").

Here are a few in base R:

str1 <- "word1.word2"

strsplit(str1, ".", fixed = TRUE)  ## Add fixed = TRUE
strsplit(str1, "[.]")              ## Make use of character classes
strsplit(str1, "\\.")              ## Escape special characters 
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
3

Try this

library(stringr)
a <- "word1.word2"
str_split(a, "\\.")
dimitris_ps
  • 5,849
  • 3
  • 29
  • 55