带竖线(管)的 strsplit [英] strsplit with vertical bar (pipe)

查看:54
本文介绍了带竖线(管)的 strsplit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里,

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

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

运行良好,但是

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

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

在这里,答案是不正确的.当我使用|and"时如何获得AA"和BB"?
提前致谢.

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

推荐答案

正如你在 ?strsplit 上看到的,strsplit 函数中的参数 split 是一个 正则表达式.因此要么你需要转义竖线(它是一个特殊字符)

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')

或者你可以选择fixed=TRUE表示split不是正则表达式

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

strsplit(r,split='|and',fixed=TRUE)

这篇关于带竖线(管)的 strsplit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆