gpt4 book ai didi

r - 粘贴()和粘贴0()之间的区别

转载 作者:行者123 更新时间:2023-12-02 07:36:56 25 4
gpt4 key购买 nike

作为R新手,有人可以解释一下paste()paste0()之间的区别吗,我从一些帖子中了解到的是

paste0("a", "b") === paste("a", "b", sep="")

即使我尝试过这样的事情

a <- c("a","b","c")
b <- c("y","w","q")
paste(a,b,sep = "_")
**output**
"a_y" "b_w" "c_q"

使用paste0()

a <- c("a","b","c")
b <- c("y","w","q")
paste0(a,b,sep = "_")
**output**
"ay_" "bw_" "cq_"

只是paste()在元素之间使用分隔符而paste0()在元素之后使用分隔符吗?

最佳答案

如上所述in this blog by Tyler Rinker :

paste has 3 arguments.

paste (..., sep = " ", collapse = NULL) The ... is the stuff you want to paste together and sep and collapse are the guys to get it done. There are three basic things I paste together:

  • A bunch of individual character strings.
  • 2 or more strings pasted element for element.
  • One string smushed together.

Here's an example of each, though not with the correct arguments

paste("A", 1, "%") #A bunch of individual character strings.

paste(1:4, letters[1:4]) #2 or more strings pasted element for element.

paste(1:10) #One string smushed together. Here's the sep/collapse rule for each:

  • A bunch of individual character strings – You want sep
  • 2 or more strings pasted element for element. – You want sep
  • One string smushed together.- Smushin requires collapse

paste0 is short for: paste(x, sep="") So it allows us to be lazier and more efficient.

paste0("a", "b") == paste("a", "b", sep="") ## [1] TRUE

关于r - 粘贴()和粘贴0()之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36279800/

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