Concatenate a vector of strings/character

RStringR Faq

R Problem Overview


If I have a vector of type character, how can I concatenate the values into string? Here's how I would do it with paste():

sdata = c('a', 'b', 'c')
paste(sdata[1], sdata[2], sdata[3], sep ='')

yielding "abc".

But of course, that only works if I know the length of sdata ahead of time.

R Solutions


Solution 1 - R

Try using an empty collapse argument within the paste function:

paste(sdata, collapse = '')

Thanks to http://twitter.com/onelinetips/status/7491806343

Solution 2 - R

Matt's answer is definitely the right answer. However, here's an alternative solution for comic relief purposes:

do.call(paste, c(as.list(sdata), sep = ""))

Solution 3 - R

You can use stri_paste function with collapse parameter from stringi package like this:

stri_paste(letters, collapse='')
## [1] "abcdefghijklmnopqrstuvwxyz" 

And some benchmarks:

require(microbenchmark)
test <- stri_rand_lipsum(100)
microbenchmark(stri_paste(test, collapse=''), paste(test,collapse=''), do.call(paste, c(as.list(test), sep="")))
Unit: microseconds
                                      expr     min       lq     mean   median       uq     max neval
           stri_paste(test, collapse = "") 137.477 139.6040 155.8157 148.5810 163.5375 226.171   100
                paste(test, collapse = "") 404.139 406.4100 446.0270 432.3250 442.9825 723.793   100
do.call(paste, c(as.list(test), sep = "")) 216.937 226.0265 251.6779 237.3945 264.8935 405.989   100

Solution 4 - R

For sdata:

gsub(", ","",toString(sdata))

For a vector of integers:

gsub(", ","",toString(c(1:10)))

Solution 5 - R

Matt Turner's answer is definitely the right answer. However, in the spirit of Ken Williams' answer, you could also do:

capture.output(cat(sdata, sep="")) 

Solution 6 - R

The library stringr 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"

str_c

Similar to paste with a collapse argument you need to specify to accomplish this:

str_c(sdata, collapse = "")
[1] "abc"

base::paste0

Though there is no obvious advantage here over paste, you could use paste0(sdata, collapse = "") from base R.


Updating the benchmark for a much longer string vector gave the following results on my machine:

set.seed(4)
x <- sample(letters, 1E6, replace = T)
microbenchmark(stri_paste(x, collapse=''), 
               paste(x,collapse=''), 
               do.call(paste, c(as.list(x), sep="")),
               stringr::str_flatten(x),
               stringr::str_c(x, collapse = ""),
               paste0(x, collapse = ""))

Unit: milliseconds
                                    expr      min        lq       mean     median        uq       max neval cld
            stri_paste(x, collapse = "")  21.1788  21.80040   23.45225   22.78430   24.4271   39.1305   100 a  
                 paste(x, collapse = "") 110.7734 114.36595  126.43277  119.02755  136.5902  187.4112   100  b 
 do.call(paste, c(as.list(x), sep = "")) 538.8329 981.80345 1090.51738 1096.33470 1213.8848 1457.5622   100   c
                 stringr::str_flatten(x)  20.6276  21.60610   23.36241   22.73915   24.2210   42.3481   100 a  
        stringr::str_c(x, collapse = "")  20.9274  21.74285   23.75466   22.73950   24.3254   36.6114   100 a  
                paste0(x, collapse = "") 110.0614 112.81175  124.15555  116.96610  130.6330  168.7199   100  b 

Also in the spirit of Ken Williams' answer:

Reduce(paste0, sdata)
[1] "abc"

Solution 7 - R

Another way would be to use glue package:

glue_collapse(glue("{sdata}"))
paste(glue("{sdata}"), collapse = '')

Solution 8 - R

Here is a little utility function that collapses a named or unnamed list of values to a single string for easier printing. It will also print the code line itself. It's from my list examples in R page.

Generate some lists named or unnamed:

# Define Lists
ls_num <- list(1,2,3)
ls_str <- list('1','2','3')
ls_num_str <- list(1,2,'3')

# Named Lists
ar_st_names <- c('e1','e2','e3')
ls_num_str_named <- ls_num_str
names(ls_num_str_named) <- ar_st_names

# Add Element to Named List
ls_num_str_named$e4 <- 'this is added'

Here is the a function that will convert named or unnamed list to string:

ffi_lst2str <- function(ls_list, st_desc, bl_print=TRUE) {
  
  # string desc
  if(missing(st_desc)){
    st_desc <- deparse(substitute(ls_list))
  }
    
  # create string
  st_string_from_list = paste0(paste0(st_desc, ':'), 
                               paste(names(ls_list), ls_list, sep="=", collapse=";" ))
  
  if (bl_print){
    print(st_string_from_list)
  }
}

Testing the function with the lists created prior:

> ffi_lst2str(ls_num)
[1] "ls_num:=1;=2;=3"
> ffi_lst2str(ls_str)
[1] "ls_str:=1;=2;=3"
> ffi_lst2str(ls_num_str)
[1] "ls_num_str:=1;=2;=3"
> ffi_lst2str(ls_num_str_named)
[1] "ls_num_str_named:e1=1;e2=2;e3=3;e4=this is added"

Testing the function with subset of list elements:

> ffi_lst2str(ls_num_str_named[c('e2','e3','e4')])
[1] "ls_num_str_named[c(\"e2\", \"e3\", \"e4\")]:e2=2;e3=3;e4=this is added"> ffi_lst2str(ls_num[2:3])
[1] "ls_num[2:3]:=2;=3"
> ffi_lst2str(ls_str[2:3])
[1] "ls_str[2:3]:=2;=3"
> ffi_lst2str(ls_num_str[2:4])
[1] "ls_num_str[2:4]:=2;=3;=NULL"
> ffi_lst2str(ls_num_str_named[c('e2','e3','e4')])
[1] "ls_num_str_named[c(\"e2\", \"e3\", \"e4\")]:e2=2;e3=3;e4=this is added"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNickView Question on Stackoverflow
Solution 1 - RMatt TurnerView Answer on Stackoverflow
Solution 2 - RKen WilliamsView Answer on Stackoverflow
Solution 3 - RbartektartanusView Answer on Stackoverflow
Solution 4 - RPatrickView Answer on Stackoverflow
Solution 5 - RC8H10N4O2View Answer on Stackoverflow
Solution 6 - RLMcView Answer on Stackoverflow
Solution 7 - RAlexBView Answer on Stackoverflow
Solution 8 - RfanView Answer on Stackoverflow