paste2: Concatenate strings (binary form)

paste2R Documentation

Concatenate strings (binary form)

Description

paste2() is a simplified version of paste0() that takes only two arguments and follows the same rules as arithmetic operations (+, *, etc...) for recycling and propagation of names, dimensions, and dimnames.

add_prefix() and add_suffix() are simple wrappers around paste2() provided for convenience and code readability.

Usage

paste2(x, y)

add_prefix(x, prefix="")
add_suffix(x, suffix="")

Arguments

x, y, prefix, suffix

Vector- or array-like objects containing strings.

Details

Unlike paste0(), paste2() only takes two arguments: x and y. It's defined as an S4 generic that dispatches on its two arguments and with methods for ordinary vectors and arrays. Bioconductor packages can define methods for other vector-like or array-like objects that contain strings.

paste2() follows the same rules as arithmetic operations (+, *, etc...) for recycling and propagation of names, dimensions, and dimnames:

  • Recycling: The longer argument "wins" i.e. the shorter argument is recycled to the length of the longer (with a warning if the length of the latter is not a multiple of the length of the former). There's one important exception to this rule: if one of the two arguments has length 0 then no recycling is performed and a zero-length vector is returned.

  • Propagation of names: The longer argument also wins. If the two arguments have the same length then the names on the first argument are propagated, if any. Otherwise the names on the second argument are propagated, if any.

  • Propagation of dimensions and dimnames: If x and y are both arrays, then they must be conformable i.e. have the same dimensions. In this case the result of paste2(x, y) is a also an array of same dimensions. Furthermore it will have dimnames(x) on it if dimnames(x) is not NULL, otherwise it will have dimnames(y) on it.

add_prefix(x, prefix="") and add_suffix(x, suffix="") are convenience wrappers that just do paste2(prefix, x) and paste2(x, suffix), respectively.

Value

If x and y are both vectors, a character vector parallel to the longer vector is returned.

If one of x or y is an array and the other one a vector, an array parallel to the input array is returned.

If x and y are both arrays (in which case they must be conformable), an array parallel to x and y is returned.

See Also

  • base::paste0 in base R.

  • showMethods for displaying a summary of the methods defined for a given generic function.

  • selectMethod for getting the definition of a specific method.

  • paste2,DelayedArray,DelayedArray-method in the DelayedArray package for an example of a specific paste2 method (defined for DelayedArray objects).

  • BiocGenerics for a summary of all the generics defined in the BiocGenerics package.

Examples

## ---------------------------------------------------------------------
## The paste2() generic and methods
## ---------------------------------------------------------------------

paste2  # note the dispatch on 'x' and 'y'
showMethods("paste2")

## ---------------------------------------------------------------------
## paste0() vs paste2()
## ---------------------------------------------------------------------

## Propagation of names:
x <- c(A="foo", B="bar")
paste0(x, "XX")  # names are lost
paste2(x, "XX")  # names are propagated
paste2(x, setNames(1:6, letters[1:6]))  # longer argument "wins"

## If 'x' or 'y' has length 0:
paste0(x, character(0))  # unname(x)
paste2(x, character(0))  # character(0)

## Propagation of dimensions and dimnames:
m <- matrix(1:12, ncol=3, dimnames=list(NULL, LETTERS[1:3]))
paste0(m, letters[1:4])  # dimensions and dimnames are lost
paste2(m, letters[1:4])  # dimensions are preserved and dimnames are
                         # propagated

## ---------------------------------------------------------------------
## add_prefix() and add_suffix()
## ---------------------------------------------------------------------

m2 <- add_prefix(m, "ID")  # same as paste2("ID", m)
add_suffix(m2, ".fasta")   # same as paste2(m2, ".fasta")

Bioconductor/BiocGenerics documentation built on Nov. 17, 2024, 6:52 p.m.