Why does strsplit("A, B . C",c(", ",". ")) not remove ". "?

Viewed 46

I'm clearly misunderstanding the documentation, so I'll make this quick. It is my understanding that strsplit("A, B . C",c(", ",". ")) should search "A, B . C" for all occurrences of ", " and strsplit it as if I had ran strsplit("A, B . C",", ") and then it should take the output of that, still in a string form, and strsplit it as if I had ran strsplit([tidied output],". "). In other words, my expected output would be list(c("A","B ","C")).

When I actually run this code, I get the following result, showing that the ". " has not been removed. What has gone wrong?

> strsplit("A, B . C",c(", ",". "))
[[1]]
[1] "A"     "B . C"

It appears to not be a regex issue. For example, setting fixed=TRUE is supposed to disable regex matching, but it doesn't change my output:

> strsplit("A, B . C",c(", ",". "),fixed = TRUE)
[[1]]
[1] "A"     "B . C"
1 Answers

When given a vector split doesn't work quite like that, see f.ex

strsplit(c("A, B . C", "A, B . C"), c(", ", ". "), fixed=TRUE )
# [[1]]
# [1] "A"     "B . C"

# [[2]]
# [1] "A, B " "C"    

It's cycled along x. If x is of length one, only the first of split will be used.
The easiest way to add more arguments to split is, I've found, to just treat it as regular regex, e.g.

strsplit("A, B . C",  "[.,] " )
# [[1]]
# [1] "A"  "B " "C" 
Related