strsplit with vertical bar (pipe)

前端 未结 1 1424
心在旅途
心在旅途 2020-12-03 17:48

Here,

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

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

Worki

1条回答
  •  天命终不由人
    2020-12-03 18:40

    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)
    

    0 讨论(0)
提交回复
热议问题