gpt4 book ai didi

r - `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`和 `stri_paste`之间的区别?

转载 作者:行者123 更新时间:2023-12-04 09:36:51 24 4
gpt4 key购买 nike

所有这些看起来非常相似的功能之间有什么区别?

最佳答案

  • stri_joinstri_cstri_paste来自于stringi包,它们是纯别名
  • str_c来自stringr,仅是stringi::stri_join,其参数ignore_null硬编码为TRUE,而stringi::stri_join默认将其设置为FALSEstringr::str_joinstr_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_joinbase::paste非常相似,下面列举了一些区别:

    1.默认情况下为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相同。

    2. NA的行为

    如果使用 NA粘贴到 stri_join,则结果为 NA,而 pasteNA转换为 "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参数的行为

    遇到长度为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"

    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

    关于r - `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`和 `stri_paste`之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53118271/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com