作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所有这些看起来非常相似的功能之间有什么区别?
最佳答案
stri_join
,stri_c
和stri_paste
来自于stringi
包,它们是纯别名str_c
来自stringr
,仅是stringi::stri_join
,其参数ignore_null
硬编码为TRUE
,而stringi::stri_join
默认将其设置为FALSE
。 stringr::str_join
是str_c
弃用的别名library(stringi)
identical(stri_join, stri_c)
# [1] TRUE
identical(stri_join, stri_paste)
# [1] TRUE
library(stringr)
str_c
# function (..., sep = "", collapse = NULL)
# {
# stri_c(..., sep = sep, collapse = collapse, ignore_null = TRUE)
# }
# <environment: namespace:stringr>
stri_join
与
base::paste
非常相似,下面列举了一些区别:
sep = ""
paste0
,但是
paste0
失去了
sep
参数。
identical(paste0("a","b") , stri_join("a","b"))
# [1] TRUE
identical(paste("a","b") , stri_join("a","b",sep=" "))
# [1] TRUE
identical(paste("a","b", sep="-"), stri_join("a","b", sep="-"))
# [1] TRUE
str_c
的行为与此处的
stri_join
相同。
NA
的行为
NA
粘贴到
stri_join
,则结果为
NA
,而
paste
将
NA
转换为
"NA"
paste0(c("a","b"),c("c",NA))
# [1] "ac" "bNA"
stri_join(c("a","b"),c("c",NA))
# [1] "ac" NA
str_c
的行为也将与
stri_join
相同
0
参数的行为
character(0)
,除非
ignore_null
设置为
FALSE
,否则将忽略该值。它与
paste
的行为不同,后者将长度
0
的值转换为
""
,因此在输出中包含2个连续的分隔符。
stri_join("a",NULL, "b")
# [1] character(0)
stri_join("a",character(0), "b")
# [1] character(0)
paste0("a",NULL, "b")
# [1] "ab"
stri_join("a",NULL, "b", ignore_null = TRUE)
# [1] "ab"
str_c("a",NULL, "b")
# [1] "ab"
paste("a",NULL, "b") # produces double space!
# [1] "a b"
stri_join("a",NULL, "b", ignore_null = TRUE, sep = " ")
# [1] "a b"
str_c("a",NULL, "b", sep = " ")
# [1] "a b"
stri_join
警告更多
paste(c("a","b"),c("c","d","e"))
# [1] "a c" "b d" "a e"
paste("a","b", sep = c(" ","-"))
# [1] "a b"
stri_join(c("a","b"),c("c","d","e"), sep = " ")
# [1] "a c" "b d" "a e"
# Warning message:
# In stri_join(c("a", "b"), c("c", "d", "e"), sep = " ") :
# longer object length is not a multiple of shorter object length
stri_join("a","b", sep = c(" ","-"))
# [1] "a b"
# Warning message:
# In stri_join("a", "b", sep = c(" ", "-")) :
# argument `sep` should be one character string; taking the first one
stri_join
更快
microbenchmark::microbenchmark(
stringi = stri_join(rep("a",1000000),rep("b",1000),"c",sep=" "),
base = paste(rep("a",1000000),rep("b",1000),"c")
)
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# stringi 88.54199 93.4477 97.31161 95.17157 96.8879 131.9737 100 a
# base 166.01024 169.7189 178.31065 171.30910 176.3055 215.5982 100 b
关于r - `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`和 `stri_paste`之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53118271/
所有这些看起来非常相似的功能之间有什么区别? 最佳答案 stri_join,stri_c和stri_paste来自于stringi包,它们是纯别名 str_c来自stringr,仅是stringi::
我是一名优秀的程序员,十分优秀!