paste2: Normal 'paste' function with additional 'collapse_last'...

View source: R/paste2.R

paste2R Documentation

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)

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 "".

Value

String. See'?paste' for details.

References

https://stackoverflow.com/a/38276239

Examples

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

miracum/misc-diztools documentation built on April 14, 2025, 7:33 a.m.