Learn R Programming

DIZtools (version 1.0.3)

paste2: Normal `paste` function with additional `collapse_last` argument.

Description

The base `paste` function but with the add on to also supply a `collapse_last` value to change the `collapse` argument at the last position. To get from "cat", "mouse", "dog" to a string "cat, mouse and dog", one simply needs to call `paste(c("cat","mouse","dog"), collapse = ", ", collapse_last = " and ")`

Usage

paste2(..., collapse = NULL, collapse_last = NULL, sep = " ", recycle0 = FALSE)

Value

String. See`?paste` for details.

Arguments

...

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

collapse

an optional character string to separate the results. Not NA_character_. When collapse is a string, the result is always a string (character of length 1).

collapse_last

(string, optional) The string to use for the last instance while collapsing. All other elements will be pasted using the normal `collapse` argument. If `collapse` is not set, `collapse_last` will be ignored.

sep

a character string to separate the terms. Not NA_character_.

recycle0

logical indicating if zero-length character arguments should result in the zero-length character(0). Note that when collapse is a string, recycle0 does not recycle to zero-length, but to "".

References

https://stackoverflow.com/a/38276239

Examples

Run this code
{
  paste2(c("cat", "mouse", "dog"),
         collapse = ", ",
         collapse_last = " and ")
  #> [1] "cat, mouse and dog"
}

Run the code above in your browser using DataLab