Function paste - Concatenate Strings
Usage
paste (…, sep = ” ”, collapse = NULL, recycle0 = FALSE)
paste0(…, collapse = NULL, recycle0 = FALSE)
Arguments
- … 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_. When collapse is a string, the result is always a string
- 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 ""
Example
## When passing a single vector, paste0 and paste work like as.character.
paste0(1:12)
paste(1:12) # same
as.character(1:12) # same
## If you pass several vectors to paste0, they are concatenated in a
## vectorized way.
(nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9))))
## paste works the same, but separates each input with a space.
## Notice that the recycling rules make every input as long as the longest input.
paste(month.abb, "is the", nth, "month of the year.")
paste(month.abb, letters)
## You can change the separator by passing a sep argument
## which can be multiple characters.
paste(month.abb, "is the", nth, "month of the year.", sep = "_*_")
## To collapse the output into a single string, pass a collapse argument.
paste0(nth, collapse = ", ")
## For inputs of length 1, use the sep argument rather than collapse
paste("1st", "2nd", "3rd", collapse = ", ") # probably not what you wanted
paste("1st", "2nd", "3rd", sep = ", ")
## You can combine the sep and collapse arguments together.
paste(month.abb, nth, sep = ": ", collapse = "; ")
## Using paste() in combination with strwrap() can be useful
## for dealing with long strings.
(title <- paste(strwrap(
"Stopping distance of cars (ft) vs. speed (mph) from Ezekiel (1930)",
width = 30), collapse = "\n"))
plot(dist ~ speed, cars, main = title)
## zero length arguments recycled as `""` -- NB: `{}` <==> character(0) here
paste({}, 1:2)
## 'recycle0 = TRUE' allows standard vectorized behaviour, i.e., zero-length
## recycling resulting in zero-length result character(0):
valid <- FALSE
val <- pi
paste("The value is", val[valid], "-- not so good!") # -> ".. value is -- not .."
paste("The value is", val[valid], "-- good: empty!", recycle0=TRUE) # -> character(0)
## When 'collapse = <string>', result is (length 1) string in all cases
paste("foo", {}, "bar", collapse = "|") # |--> "foo bar"
paste("foo", {}, collapse = "|", recycle0 = TRUE) # |--> ""
## If all arguments are empty (and collapse a string), "" results always
paste( collapse = "|")
paste( collapse = "|", recycle0 = TRUE)
paste({}, collapse = "|")
paste({}, collapse = "|", recycle0 = TRUE)