rrs中strsplit()的奇怪行为?

时间:2014-08-01 09:42:18

标签: r strsplit

我想使用 strsplit()将字符串x = "a,b,"(最后一位逗号)拆分为向量c("a","b","")

结果是:

>strsplit(x,',')
[[1]]
[1] "a" "b"

我希望有第三个组件(空字符串或NULL)。

函数read.csv(x)可以管理它,但我认为strsplit()应该按照我的预期行事。 Python给出了c("a","b","")

也许strsplit()有一些我不知道的选项?

1 个答案:

答案 0 :(得分:5)

它是如何工作的,并记录在help(strsplit)中:

 Note that this means that if there is a match at the beginning of
 a (non-empty) string, the first element of the output is ‘""’, but
 if there is a match at the end of the string, the output is the
 same as with the match removed.

您可能希望使用str_split包中的stringr

> require(stringr)
> str_split("a,b,",",")
[[1]]
[1] "a" "b" "" 

> str_split("a,b",",")
[[1]]
[1] "a" "b"

> str_split(",a,b",",")
[[1]]
[1] ""  "a" "b"

> str_split(",a,b,,,",",")
[[1]]
[1] ""  "a" "b" ""  ""  ""