举个例子,看下面提取Twitter账户的操作。目标是获得一个类似于tweets
的字符串,只包含用逗号隔开的账户名。使用str_replace_all
函数替换没有匹配到的内容时会返回空向量,这可能会在后续操作中导致一些意想不到的错误。
library(purrr)
library(stringr)
tweets <- c(
"",
"This tweet has no handles",
"This is a tweet for @you",
"This is another tweet for @you and @me",
"This, @bla, is another tweet for @me and @you"
)
mention_rx <- "@\\w+"
这是我的第一次尝试:
map_chr(tweets, ~str_c(str_extract_all(.x, mention_rx)[[1]], collapse = ", "))
#> Error: Result 1 must be a single string, not a character vector of length 0
然后我尝试了一些东西:
mentions <- map(tweets, ~str_c(str_extract_all(.x, mention_rx)[[1]], collapse = ", "))
mentions
#> [[1]]
#> character(0)
#>
#> [[2]]
#> character(0)
#>
#> [[3]]
#> [1] "@you"
#>
#> [[4]]
#> [1] "@you, @me"
#>
#> [[5]]
#> [1] "@bla, @me, @you"
as.character(mentions)
#> [1] "character(0)" "character(0)" "@you" "@you, @me"
#> [5] "@bla, @me, @you"
直到我意识到 paste
也可以在这里使用:
map_chr(tweets, ~paste(str_extract_all(.x, mention_rx)[[1]], collapse = ", "))
#> "" "" "@you" "@you, @me" "@bla, @me, @you"
我的问题是:
- 有没有更优雅的方法来解决这个问题?
- 为什么
str_c
和带有相同collapse
参数的paste
表现不同? - 为什么
as.character
和map_chr
不能把长度为零的字符向量识别为空字符串,但paste
可以?
我在以下参考资料中找到了一些很好的内容:str(i)_c、paste,以及它们之间的区别;但它们都没有涉及空字符串的情况。
stringr
函数进行映射...即使在那种情况下,我也不确定是否有正当理由。你对“为什么”问题可能的答案有任何线索吗? - Fons MA