【问题标题】:Strange behavior of strsplit() in R?R中strsplit()的奇怪行为?
【发布时间】:2014-08-01 09:42:18
【问题描述】:

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

结果是:

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

我想要第三个组件(空字符串或 NULL)。

函数read.csv(x) 可以解决这个问题,但我仍然认为strsplit() 的行为应该符合我的预期。 Python 给出c("a","b","")

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

【问题讨论】:

  • 您可以通过x <- stringr::str_pad("a,b,", 5, "right"); strsplit(x, ",| ")[[1]] 获得您想要的结果。但是,是的,strsplit 就是这样工作的。
  • 你可以使用来自stringrstr_split_fixed(x, ",", 3)

标签: r strsplit


【解决方案1】:

这就是它的工作原理,并记录在 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.

您可能想使用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" ""  ""  "" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多