Joins two or more vectors element-wise into a single character vector, optionally inserting sep between input vectors. If collapse is not NULL, it will be inserted between elements of the result, returning a character vector of length 1.

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.

Details

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.

See also

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

Examples

str_c("Letter: ", letters)
#> [1] "Letter: a" "Letter: b" "Letter: c" "Letter: d" "Letter: e" "Letter: f" #> [7] "Letter: g" "Letter: h" "Letter: i" "Letter: j" "Letter: k" "Letter: l" #> [13] "Letter: m" "Letter: n" "Letter: o" "Letter: p" "Letter: q" "Letter: r" #> [19] "Letter: s" "Letter: t" "Letter: u" "Letter: v" "Letter: w" "Letter: x" #> [25] "Letter: y" "Letter: z"
str_c("Letter", letters, sep = ": ")
#> [1] "Letter: a" "Letter: b" "Letter: c" "Letter: d" "Letter: e" "Letter: f" #> [7] "Letter: g" "Letter: h" "Letter: i" "Letter: j" "Letter: k" "Letter: l" #> [13] "Letter: m" "Letter: n" "Letter: o" "Letter: p" "Letter: q" "Letter: r" #> [19] "Letter: s" "Letter: t" "Letter: u" "Letter: v" "Letter: w" "Letter: x" #> [25] "Letter: y" "Letter: z"
str_c(letters, " is for", "...")
#> [1] "a is for..." "b is for..." "c is for..." "d is for..." "e is for..." #> [6] "f is for..." "g is for..." "h is for..." "i is for..." "j is for..." #> [11] "k is for..." "l is for..." "m is for..." "n is for..." "o is for..." #> [16] "p is for..." "q is for..." "r is for..." "s is for..." "t is for..." #> [21] "u is for..." "v is for..." "w is for..." "x is for..." "y is for..." #> [26] "z is for..."
str_c(letters[-26], " comes before ", letters[-1])
#> [1] "a comes before b" "b comes before c" "c comes before d" "d comes before e" #> [5] "e comes before f" "f comes before g" "g comes before h" "h comes before i" #> [9] "i comes before j" "j comes before k" "k comes before l" "l comes before m" #> [13] "m comes before n" "n comes before o" "o comes before p" "p comes before q" #> [17] "q comes before r" "r comes before s" "s comes before t" "t comes before u" #> [21] "u comes before v" "v comes before w" "w comes before x" "x comes before y" #> [25] "y comes before z"
str_c(letters, collapse = "")
#> [1] "abcdefghijklmnopqrstuvwxyz"
str_c(letters, collapse = ", ")
#> [1] "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z"
# Missing inputs give missing outputs str_c(c("a", NA, "b"), "-d")
#> [1] "a-d" NA "b-d"
# Use str_replace_NA to display literal NAs: str_c(str_replace_na(c("a", NA, "b")), "-d")
#> [1] "a-d" "NA-d" "b-d"