str_c()
combines multiple character vectors into a single character
vector. It's very similar to paste0()
but uses tidyverse recycling and
NA
rules.
One way to understand how str_c()
works is picture a 2d matrix of strings,
where each argument forms a column. sep
is inserted between each column,
and then each row is combined together into a single string. If collapse
is set, it's inserted between each row, and then the result is again
combined, this time into a single string.
str_c(..., sep = "", collapse = NULL)
One or more character vectors.
NULL
s are removed; scalar inputs (vectors of length 1) are recycled to
the common length of vector inputs.
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 dplyr::coalesce()
or str_replace_na()
to convert to
the desired value.
String to insert between input vectors.
Optional string used to combine output into single
string. Generally better to use str_flatten()
if you needed this
behaviour.
If collapse = NULL
(the default) a character vector with
length equal to the longest input. If collapse
is a string, a character
vector of length 1.
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"
# Differences from paste() ----------------------
# Missing inputs give missing outputs
str_c(c("a", NA, "b"), "-d")
#> [1] "a-d" NA "b-d"
paste0(c("a", NA, "b"), "-d")
#> [1] "a-d" "NA-d" "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"
# Uses tidyverse recycling rules
if (FALSE) str_c(1:2, 1:3) # \dontrun{} # errors
paste0(1:2, 1:3)
#> [1] "11" "22" "13"
str_c("x", character())
#> character(0)
paste0("x", character())
#> [1] "x"