str_c

0th

Percentile

Join multiple strings into a single string.

To understand how str_c works, you need to imagine that you are building up a matrix of strings. Each input argument forms a column, and is expanded to the length of the longest argument, using the usual recyling rules. The sep string is inserted between each column. If collapse is NULL each row is collapsed into a single string. If non-NULL that string is inserted at the end of each row, and the entire matrix collapsed to a single string.

Usage
str_c(..., sep = "", collapse = NULL)
Arguments
...

One or more character vectors. Zero length arguments are removed. Short arguments are recycled to the length of the longest.

Like most other R functions, missing values are "infectious": whenever a missing value is combined with another string the result will always be missing. Use str_replace_na() to convert NA to "NA"

sep

String to insert between input vectors.

collapse

Optional string used to combine input vectors into single string.

Value

If collapse = NULL (the default) a character vector with length equal to the longest input string. If collapse is non-NULL, a character vector of length 1.

See Also

paste() for equivalent base R functionality, and stringi::stri_join() which this function wraps

Aliases
  • str_c
Examples
# NOT RUN {
str_c("Letter: ", letters)
str_c("Letter", letters, sep = ": ")
str_c(letters, " is for", "...")
str_c(letters[-26], " comes before ", letters[-1])

str_c(letters, collapse = "")
str_c(letters, collapse = ", ")

# Missing inputs give missing outputs
str_c(c("a", NA, "b"), "-d")
# Use str_replace_NA to display literal NAs:
str_c(str_replace_na(c("a", NA, "b")), "-d")
# }
Documentation reproduced from package stringr, version 1.3.1, License: GPL-2 | file LICENSE

Community examples

richie@datacamp.com at Jan 25, 2017 stringr v1.1.0

There are two differences between `str_c()` and [`paste0()`](https://www.rdocumentation.org/packages/base/topics/paste). ## `str_c()` treats missing values properly [`paste0()`](https://www.rdocumentation.org/packages/base/topics/paste) treats missing values as though they were the string `"NA"`, whereas `str_c()` respects their missingness. ```{r} x <- LETTERS x[x %in% c("A", "E", "I", "O", "U")] <- NA y <- letters y[c(TRUE, FALSE, FALSE)] <- NA stringr::str_c(x, y) paste0(x, y) ``` ## `str_c()` warns on inexact recycling If inputs are different lengths, and not exact multiples of each other, `str_c()` follows the usual vector recycling convention of throwing a warning. ```{r} paste0(month.abb, letters) stringr::str_c(month.abb, letters) ```