File: str_c.Rd

package info (click to toggle)
r-cran-stringr 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,032 kB
  • sloc: javascript: 11; sh: 9; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 2,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/c.R
\name{str_c}
\alias{str_c}
\title{Join multiple strings into one string}
\usage{
str_c(..., sep = "", collapse = NULL)
}
\arguments{
\item{...}{One or more character vectors.

\code{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 \code{\link[dplyr:coalesce]{dplyr::coalesce()}} or \code{\link[=str_replace_na]{str_replace_na()}} to convert to
the desired value.}

\item{sep}{String to insert between input vectors.}

\item{collapse}{Optional string used to combine output into single
string. Generally better to use \code{\link[=str_flatten]{str_flatten()}} if you needed this
behaviour.}
}
\value{
If \code{collapse = NULL} (the default) a character vector with
length equal to the longest input. If \code{collapse} is a string, a character
vector of length 1.
}
\description{
\code{str_c()} combines multiple character vectors into a single character
vector. It's very similar to \code{\link[=paste0]{paste0()}} but uses tidyverse recycling and
\code{NA} rules.

One way to understand how \code{str_c()} works is picture a 2d matrix of strings,
where each argument forms a column. \code{sep} is inserted between each column,
and then each row is combined together into a single string. If \code{collapse}
is set, it's inserted between each row, and then the result is again
combined, this time into a single string.
}
\examples{
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 = ", ")

# Differences from paste() ----------------------
# Missing inputs give missing outputs
str_c(c("a", NA, "b"), "-d")
paste0(c("a", NA, "b"), "-d")
# Use str_replace_NA to display literal NAs:
str_c(str_replace_na(c("a", NA, "b")), "-d")

# Uses tidyverse recycling rules
\dontrun{str_c(1:2, 1:3)} # errors
paste0(1:2, 1:3)

str_c("x", character())
paste0("x", character())
}