简体   繁体   English

R中strsplit()的奇怪行为?

[英]Strange behavior of strsplit() in R?

I would like to split the string x = "a,b," (comma at the last place) into the vector c("a","b","") using strsplit() . 我想使用strsplit()将字符串x = "a,b," (最后一个逗号)拆分为向量c("a","b","")

The result is: 结果是:

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

I would like the have the third component (empty string or NULL). 我想要第三个组件(空字符串或NULL)。

The function read.csv(x) can manage that, but still I think that strsplit() should behave as I expected. 函数read.csv(x)可以管理该问题,但我仍然认为strsplit()行为应与我预期的一样。 Python gives c("a","b","") . Python给出c("a","b","")

Maybe there is some option of strsplit() I do not know? 也许我不知道有一些strsplit()选项吗?

That's how it works and is documented in help(strsplit): 这就是它的工作方式,并记录在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. 

You might want to use str_split from the stringr package: 您可能要使用stringr包中的str_split

> 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" ""  ""  "" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM