Learn R Programming

stringr (version 1.5.1)

str_flatten: Flatten a string

Description

str_flatten() reduces a character vector to a single string. This is a summary function because regardless of the length of the input x, it always returns a single string.

str_flatten_comma() is a variation designed specifically for flattening with commas. It automatically recognises if last uses the Oxford comma and handles the special case of 2 elements.

Usage

str_flatten(string, collapse = "", last = NULL, na.rm = FALSE)

str_flatten_comma(string, last = NULL, na.rm = FALSE)

Value

A string, i.e. a character vector of length 1.

Arguments

string

Input vector. Either a character vector, or something coercible to one.

collapse

String to insert between each piece. Defaults to "".

last

Optional string to use in place of the final separator.

na.rm

Remove missing values? If FALSE (the default), the result will be NA if any element of string is NA.

Examples

Run this code
str_flatten(letters)
str_flatten(letters, "-")

str_flatten(letters[1:3], ", ")

# Use last to customise the last component
str_flatten(letters[1:3], ", ", " and ")

# this almost works if you want an Oxford (aka serial) comma
str_flatten(letters[1:3], ", ", ", and ")

# but it will always add a comma, even when not necessary
str_flatten(letters[1:2], ", ", ", and ")

# str_flatten_comma knows how to handle the Oxford comma
str_flatten_comma(letters[1:3], ", and ")
str_flatten_comma(letters[1:2], ", and ")

Run the code above in your browser using DataLab