10

Here,

> r<-c("AAandBB", "BBandCC")
> strsplit(as.character(r),'and')
[[1]]
[1] "AA" "BB"

[[2]]
[1] "BB" "CC"

Working well, but

> r<-c("AA|andBB", "BB|andCC")
> strsplit(as.character(r),'|and')
[[1]]
[1] "A" "A" "|" ""  "B" "B"

[[2]]
[1] "B" "B" "|" ""  "C" "C"

Here, the answer is not correct. How to get "AA" and "BB", when I use '|and'?
Thanks in advance.

RockScience
  • 17,932
  • 26
  • 89
  • 125
ramesh
  • 1,187
  • 7
  • 19
  • 42
  • 2
    There is no need to use as.character() in strsplit function. :) – bartektartanus Apr 21 '14 at 08:09
  • possible duplicate of [character "|" in R](http://stackoverflow.com/questions/6382425/character-in-r) – RockScience Apr 22 '14 at 03:41
  • found this question because i tried splitting on `"\|"`, leading to the error, `'\|' is an unrecognized escape...`. I just needed to add a second backslash `"\\|"` as indicated by @RockScience. Thanks!! – flies Mar 11 '16 at 15:49

1 Answers1

30

As you can read on ?strsplit, the argument split in function strsplit is a regular expression. Hence either you need to escape the vertical bar (it is a special character)

strsplit(r,split='\\|and')

or you can choose fixed=TRUE to indicate that split is not a regular expression

strsplit(r,split='|and',fixed=TRUE)
RockScience
  • 17,932
  • 26
  • 89
  • 125