代码之家  ›  专栏  ›  技术社区  ›  moodymudskipper

“粘贴”、“str\u c”、“str\u join”、“stri\u join”、“stri\u c”、“stri\u paste”之间的区别?

  •  2
  • moodymudskipper  · 技术社区  · 6 年前

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

    1 回复  |  直到 6 年前
        1
  •  10
  •   moodymudskipper    6 年前
    • stri_join , stri_c ,和 stri_paste stringi 是纯化名

    • str_c 来自 stringr 只是 stringi::stri_join 带参数 ignore_null TRUE 虽然 strii::stri\u连接 设置为 FALSE 默认情况下。 stringr::str_join 是的不推荐使用的别名 结构

    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>
    

    连续连接 非常类似于 base::paste 以下列举了一些不同之处:


    1. sep = "" 默认情况下

    paste0 默认情况下,但是 粘贴0 失去了它的 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
    

    会表现得像 在这里。


    NA

    如果粘贴到 不适用 使用 连续连接 ,结果是 不适用 paste 皈依者 不适用 "NA"

    paste0(c("a","b"),c("c",NA))
    # [1] "ac"  "bNA"
    stri_join(c("a","b"),c("c",NA))
    # [1] "ac" NA
    

    会表现得像 连续连接 这里也是


    0

    当遇到长度0值时, character(0) 被退回,除非 设置为 错误的 粘贴 这将转换长度 0 价值 ""

    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. 连续连接 更多警告

    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
    

    速度更快

    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