paste,str_c,str_join,stri_join,stri_c,stri_paste之间的区别? [英] Difference between `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`, `stri_paste`?
问题描述
所有这些看起来非常相似的功能之间有什么区别?
What are the differences between all of these functions that seem very similar ?
推荐答案
-
stri_join
,stri_c
和stri_paste
来自软件包stringi
,它们是纯别名stri_join
,stri_c
, andstri_paste
come from packagestringi
and are pure aliasesstr_c
来自stringr
,并且只是stringi::stri_join
,其参数ignore_null
硬编码为TRUE
,而stringi::stri_join
将其设置为FALSE
默认.stringr::str_join
是str_c
str_c
comes fromstringr
and is juststringi::stri_join
with a parameterignore_null
hardcoded toTRUE
whilestringi::stri_join
has it set toFALSE
by default.stringr::str_join
is a deprecated alias forstr_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
非常相似,以下列举了一些区别:stri_join
is very similar tobase::paste
with a few differences enumerated below:1.
sep = ""
默认情况下1.
sep = ""
by default因此,默认情况下它的行为更像
paste0
,但是paste0
失去了它的sep
参数.So it behaves more like
paste0
by default, butpaste0
lost itssep
argument.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
一样.str_c
will behave just likestri_join
here.2.
NA
2. Behavior with
NA
如果使用
stri_join
粘贴到NA
,则结果为NA
,而paste
将NA
转换为"NA"
if you paste to
NA
usingstri_join
, the result isNA
, whilepaste
convertsNA
to"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
一样3.长度为
0
参数的行为3. Behavior with length
0
arguments遇到长度为0的值时,将返回
character(0)
,除非将ignore_null
设置为FALSE
,否则将忽略该值.它与paste
的行为不同,后者将长度0
的值转换为""
,因此在输出中包含2个连续的分隔符.When a length 0 value is encountered,
character(0)
is returned, except ifignore_null
is set toFALSE
, then the value is ignored. It is different from the behavior ofpaste
which would convert the length0
value to""
and thus contain 2 consecutive separators in the output.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"
4.
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
5.
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
这篇关于paste,str_c,str_join,stri_join,stri_c,stri_paste之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!