前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >paste和paste0的区别

paste和paste0的区别

原创
作者头像
用户10331357
发布2023-02-07 13:33:20
6990
发布2023-02-07 13:33:20
举报

从帮助文档看,paste和paste0只有< sep = " " >这一个参数有区别

代码语言:text
复制
paste (..., sep = " ", collapse = NULL, recycle0 = FALSE)
paste0(...,            collapse = NULL, recycle0 = FALSE)
  • 首先看一下这两个函数都是怎么使用的,使用帮助文档的数据进行解析
  • paste函数
代码语言:text
复制
paste(month.abb, nth)
[1] "Jan 1st"  "Feb 2nd"  "Mar 3rd"  "Apr 4th"  "May 5th"  "Jun 6th"  "Jul 7th"  "Aug 8th"  "Sep 9th"  "Oct 10th"
[11] "Nov 11th" "Dec 12th"
paste(month.abb, nth, sep = ": ")
[1] "Jan: 1st"  "Feb: 2nd"  "Mar: 3rd"  "Apr: 4th"  "May: 5th"  "Jun: 6th"  "Jul: 7th"  "Aug: 8th"  "Sep: 9th" 
[10] "Oct: 10th" "Nov: 11th" "Dec: 12th"
paste(month.abb, nth,collapse = "; ")
[1] "Jan 1st; Feb 2nd; Mar 3rd; Apr 4th; May 5th; Jun 6th; Jul 7th; Aug 8th; Sep 9th; Oct 10th; Nov 11th; Dec 12th"
paste(month.abb, nth, sep = ": ", collapse = "; ")
[1] "Jan: 1st; Feb: 2nd; Mar: 3rd; Apr: 4th; May: 5th; Jun: 6th; Jul: 7th; Aug: 8th; Sep: 9th; Oct: 10th; Nov: 11th; Dec: 12th"

因而得出结论

paste函数变量之间连接时空格作为默认的分隔符

参数sep = ": "变量之间连接使用 ": " 作为分割符

参数collapse = "; " 当所有的变量坍缩为一个向量时,"; "作为变量连接之后的分隔符

  • 以同样的方式验证paste0函数
代码语言:text
复制
paste0(month.abb, nth)
[1] "Jan1st"  "Feb2nd"  "Mar3rd"  "Apr4th"  "May5th"  "Jun6th"  "Jul7th"  "Aug8th"  "Sep9th"  "Oct10th" "Nov11th"
[12] "Dec12th"
paste0(month.abb, nth, sep = " ")
[1] "Jan1st "  "Feb2nd "  "Mar3rd "  "Apr4th "  "May5th "  "Jun6th "  "Jul7th "  "Aug8th "  "Sep9th "  "Oct10th "
[11] "Nov11th " "Dec12th "
paste0(month.abb, nth, sep = ": ")
[1] "Jan1st: "  "Feb2nd: "  "Mar3rd: "  "Apr4th: "  "May5th: "  "Jun6th: "  "Jul7th: "  "Aug8th: "  "Sep9th: " 
[10] "Oct10th: " "Nov11th: " "Dec12th: "
paste0(month.abb, nth,collapse = "; ")
[1] "Jan1st; Feb2nd; Mar3rd; Apr4th; May5th; Jun6th; Jul7th; Aug8th; Sep9th; Oct10th; Nov11th; Dec12th"
paste0(month.abb, nth, sep = ": ", collapse = "; ")
[1] "Jan1st: ; Feb2nd: ; Mar3rd: ; Apr4th: ; May5th: ; Jun6th: ; Jul7th: ; Aug8th: ; Sep9th: ; Oct10th: ; Nov11th: ; Dec12th: "

paste0函数变量之间连接时没有分隔符

使用参数sep = " "变量之间也不会出现空格,反而会在变量最后添加分割符

使用参数sep = ": "会在变量最后添加

参数collapse = " " 当所有的变量坍缩为一个向量时," "作为变量连接之后的分隔符

  • 因此最后得出结论: sep = " "作为paste函数变量之间连接的分隔符,paste函数变量之间连接无分隔符,如果使用了,只会在变量最后加上分隔符 参数collapse = " " 当所有的变量坍缩为一个向量时," "作为变量连接之后的分隔符,paste函数和paste0函数使用效果相同

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档