Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strsplit with vertical bar (pipe)

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.

like image 660
ramesh Avatar asked Apr 21 '14 07:04

ramesh


1 Answers

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)
like image 171
RockScience Avatar answered Sep 22 '22 02:09

RockScience