How to use the strsplit function with a period

user3022875 picture user3022875 · Oct 31, 2014 · Viewed 31.8k times · Source

I would like to split the following string by its periods. I tried strsplit() with "." in the split argument, but did not get the result I want.

s <- "I.want.to.split"
strsplit(s, ".")
[[1]]
 [1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

The output I want is to split s into 4 elements in a list, as follows.

[[1]]
[1] "I"     "want"  "to"    "split"

What should I do?

Answer

Rich Scriven picture Rich Scriven · Oct 31, 2014

When using a regular expression in the split argument of strsplit(), you've got to escape the . with \\., or use a charclass [.]. Otherwise you use . as its special character meaning, "any single character".

s <- "I.want.to.split"
strsplit(s, "[.]")
# [[1]]
# [1] "I"     "want"  "to"    "split"

But the more efficient method here is to use the fixed argument in strsplit(). Using this argument will bypass the regex engine and search for an exact match of ".".

strsplit(s, ".", fixed = TRUE)
# [[1]]
# [1] "I"     "want"  "to"    "split"

And of course, you can see help(strsplit) for more.