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.