tidyverse
The stringr package has a few, fast ways you could accomplish this.
str_flatten
By default will collapse your character vector with no spaces, but does have collapse argument as well:
str_flatten(sdata) # [1] "abc"
Also has an optional last argument to use in place of the final separator.
str_c
Similar to paste with a collapse argument you need to specify to accomplish this:
str_c(sdata, collapse = "") # [1] "abc"
str_flatten_comma
New as of stringr 1.5.0 if you want a comma delimited collapse. Here the last argument recognizes the Oxford comma:
str_flatten_comma(sdata) # similar to base R toString() # [1] "a, b, c" ## handling Oxford comma str_flatten_comma(sdata, last = ", and ") # [1] "a, b, and c" str_flatten_comma(sdata[1:2], last = ", and ") # (removes comma) # [1] "a and b"
stringfish (faster)
For most cases stringi and stringr will provide enough speed, but if you need something faster consider the stringfish package:
library(stringfish) sf_collapse(sdata, collapse = '') # [1] "abc"
Base R
For completeness, you could use paste0, though there is no obvious advantage here over paste:
paste0(sdata, collapse = "")
Benchmark
Across vector sizes of 10K, 100K, 1M and 10M stringfish yields consistently faster results:
expression string_length min median `itr/sec` `gc/sec` n_itr n_gc <bch:expr> <dbl> <bch:tm> <bch:tm> <dbl> <dbl> <int> <dbl> 1 sf_collapse 10000 149µs 163.8µs 6020. 0 3009 0 2 str_flatten 10000 166.8µs 174.2µs 5527. 0 2762 0 3 stri_paste 10000 163.9µs 176µs 5517. 0 2757 0 4 str_c 10000 186µs 200.3µs 4954. 0 2476 0 5 paste 10000 606.8µs 677.4µs 1472. 2.06 715 1 6 paste0 10000 606.3µs 681.9µs 1449. 0 725 0 7 sf_collapse 100000 1.48ms 1.55ms 643. 0 322 0 8 stri_paste 100000 1.81ms 1.96ms 490. 0 245 0 9 str_flatten 100000 1.81ms 2.04ms 486. 0 244 0 10 str_c 100000 1.84ms 2.03ms 480. 0 241 0 11 paste0 100000 6.24ms 6.73ms 147. 2.10 70 1 12 paste 100000 6.37ms 6.98ms 142. 0 72 0 13 sf_collapse 1000000 16.02ms 16.88ms 59.4 0 30 0 14 str_flatten 1000000 19.45ms 20.02ms 49.7 0 25 0 15 stri_paste 1000000 19.28ms 20.07ms 49.6 0 25 0 16 str_c 1000000 19.8ms 20.77ms 47.9 0 24 0 17 paste 1000000 64.06ms 65.41ms 15.3 0 8 0 18 paste0 1000000 64.54ms 65.75ms 15.1 0 8 0 19 sf_collapse 10000000 167.88ms 169.53ms 5.91 0 3 0 20 str_c 10000000 199.67ms 200.04ms 4.96 0 3 0 21 stri_paste 10000000 205.17ms 210.69ms 4.76 0 3 0 22 str_flatten 10000000 216.98ms 217.04ms 4.60 0 3 0 23 paste0 10000000 690.85ms 690.85ms 1.45 0 1 0 24 paste 10000000 767.12ms 767.12ms 1.30 1.30 1 1
Benchmark Code
library(bench) library(stringr) library(stringi) library(stringfish) set.seed(4) results <- press( string_length = c(1E4, 1E5, 1E6, 1E7), { x <- sample(letters, string_length, replace = T) mark( stri_paste = stri_paste(x, collapse=''), paste = paste(x,collapse=''), str_flatten = str_flatten(x), str_c = str_c(x, collapse = ""), paste0 = paste0(x, collapse = ""), sf_collapse = sf_collapse(x, collapse = ""), memory = FALSE) } ) sort_by(results, ~ list(string_length, - `itr/sec`)) |> subset(select = c(1:5, 7:9)) |> print(n = 24)