R语言中的函数25:paste,paste0

文章目录

  • 介绍
  • paste0()
    • 实例
  • paste()
    • 实例

介绍

paste0()和paste()函数都可以实现对字符串的连接,paste0是paste的简化版。

paste0()

paste (..., sep = " ", collapse = NULL, recycle0 = FALSE)

  • one or more R objects, to be converted to character vectors.

  • sep
    a character string to separate the terms. Not NA_character_.

  • collapse
    an optional character string to separate the results. Not NA_character_.

  • recycle0
    logical indicating if zero-length character arguments should lead to the zero-length character(0) after the sep-phase (which turns into “” in the collapse-phase, i.e., when collapse is not NULL).

实例

> paste0('a','b','c')
[1] "abc"
> paste0(c('a','b'),c('c','d'))
[1] "ac" "bd"
> paste0(c('a','b'),c('c','d'),collapse = '+')
[1] "ac+bd"

paste()

相比于paste0,paste()函数提供了sep参数来制定连接符。

注意:在对向量间元素进行连接时使用sep参数,在将向量内全部元素连接时需要使用collapse 参数

实例

paste('a','b','c')
# [1] "a b c"
paste(c('a','b'),c('c','d'))
# [1] "a c" "b d"
paste(c('a','b'),c('c','d'),sep='+')
# [1] "a+c" "b+d"
paste(c('a','b'),c('c','d'),collapse = '+')
# [1] "a+c" "b+d"

你可能感兴趣的:(#,R语言中的函数,r语言,前端,开发语言)