paste,str_c,str_join,stri_join,stri_c,stri_paste之间的区别? [英] Difference between `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`, `stri_paste`?

查看:201
本文介绍了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_joinstri_cstri_paste来自软件包 stringi ,它们是纯别名

    • stri_join, stri_c, and stri_paste come from package stringi and are pure aliases

      str_c来自 stringr ,并且只是stringi::stri_join,其参数ignore_null硬编码为TRUE,而stringi::stri_join将其设置为FALSE默认. stringr::str_joinstr_c

      str_c comes from stringr and is just stringi::stri_join with a parameter ignore_null hardcoded to TRUE while stringi::stri_join has it set to FALSE by default. stringr::str_join is a deprecated alias for 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_joinbase::paste非常相似,以下列举了一些区别:

      stri_join is very similar to base::paste with a few differences enumerated below:

      1. sep = ""默认情况下

      1. sep = "" by default

      因此,默认情况下它的行为更像paste0,但是paste0失去了它的sep参数.

      So it behaves more like paste0 by default, but paste0 lost its sep 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 like stri_join here.

      2. NA

      2. Behavior with NA

      如果使用stri_join粘贴到NA,则结果为NA,而pasteNA转换为"NA"

      if you paste to NA using stri_join, the result is NA, while paste converts NA 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 if ignore_null is set to FALSE, then the value is ignored. It is different from the behavior of paste which would convert the length 0 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆