作为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()
在元素之后使用分隔符吗?
发布于 2016-03-29 17:31:37
正如in this blog by Tyler Rinker所解释的那样
paste
有3个参数。
...
是你想要粘贴在一起的东西,而sep和paste (..., sep = " ", collapse = NULL)
是完成它的人。有三个基本的东西我粘贴在一起:
下面是每种方法的一个示例,尽管没有正确的参数
paste("A", 1, "%")
#一串单独的字符串。
paste(1:4, letters[1:4])
#2或更多字符串为元素粘贴了元素。
paste(1:10)
#一个字符串被挤在一起。下面是每种情况下的sep/collapse规则:
paste0
是:paste(x, sep="")
的缩写,所以它可以让我们变得更懒、更有效率。
paste0("a", "b") == paste("a", "b", sep="") ## [1] TRUE
发布于 2017-07-16 12:37:23
简而言之,
paste()
类似于使用分离因子的串联,而
paste0()
类似于使用分离因子的附加函数。
在上面的讨论中添加更多的参考,下面的试用可能有助于避免混淆:
> paste("a","b") #Here default separation factor is " " i.e. a space
[1] "a b"
> paste0("a","b") #Here default separation factor is "" i.e a null
[1] "ab"
> paste("a","b",sep="-")
[1] "a-b"
> paste0("a","b",sep="-")
[1] "ab-"
> paste(1:4,"a")
[1] "1 a" "2 a" "3 a" "4 a"
> paste0(1:4,"a")
[1] "1a" "2a" "3a" "4a"
> paste(1:4,"a",sep="-")
[1] "1-a" "2-a" "3-a" "4-a"
> paste0(1:4,"a",sep="-")
[1] "1a-" "2a-" "3a-" "4a-"
发布于 2016-06-11 13:18:23
让我用一个简单的词来描述它..paste0
将自动排除串联中的空格。
例如,我想创建一个训练和测试路径。代码如下。
> Curr_date=format(Sys.Date(),"%d-%b-%y")
> currentTrainPath = paste("Train_",Curr_date,".RData")
> currentTrainPath
[1] "Train_ 11-Jun-16 .RData"
> Curr_date=format(Sys.Date(),"%d-%b-%y")
> currentTrainPath = paste0("Train_",Curr_date,".RData")
> currentTrainPath
[1] "Train_11-Jun-16.RData"
https://stackoverflow.com/questions/36279800
复制相似问题