str_flatten

The function stringr::str_flatten() allows you to create a single string from a character vector.

Tidyverse reference page

Flatten

By default str_flatten() will add no spaces/separators when flattening.

#Create vector
vec <- c("A", "C", "G", "T")
#Flatten vector
vec |> stringr::str_flatten()
[1] "ACGT"

You can pipe the output for str_c() to str_flatten().

#Create vectors
vec1 <- c("a", "b", "c")
vec2 <- c("x", "y", "z")
#Combine then flatten
stringr::str_c(vec1, vec2) |> stringr::str_flatten()
[1] "axbycz"

Collapse and last

The individual vector elements can be separated in the out string with the collapse= option.

#Create vector
vec <- c("A", "C", "G", "T")
#Flatten with , separator
stringr::str_flatten(vec, collapse = ",")
[1] "A,C,G,T"

You can specify a final separator with last =. Additionally, separators can contain as many characters as you would like.

stringr::str_flatten(vec, collapse = ", ", last = ", and ")
[1] "A, C, G, and T"