348

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Nick
  • 21,555
  • 18
  • 47
  • 50

8 Answers8

600

Try using an empty collapse argument within the paste function:

paste(sdata, collapse = '')

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

Matt Turner
  • 6,141
  • 1
  • 16
  • 9
  • 13
    Just a note that if `sdata` can contain either strings which are all of the same length or of variable lengths then `paste(sdata, sep = '', collapse = '')` should be used to avoid unexpected results. – zelanix Jan 19 '14 at 22:03
51

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 = ""))
zx8754
  • 52,746
  • 12
  • 114
  • 209
Ken Williams
  • 22,756
  • 10
  • 85
  • 147
  • 7
    you really could have worked an apply() statement in there. If you do I'll up vote you ;) – JD Long Jan 20 '10 at 21:17
  • this would actually be the most elegant solution if the `collapse` parameter didn’t exist. so not much comedic relief if you recently actually had to do something very similar :) – flying sheep Mar 06 '15 at 09:03
14

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
bartektartanus
  • 15,284
  • 6
  • 74
  • 102
8

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"

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)
[1] "a, b, c"

str_flatten_comma(sdata[1:2], last = " and ")
[1] "a and b"

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"
LMc
  • 12,577
  • 3
  • 31
  • 43
5

For sdata:

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

For a vector of integers:

gsub(", ", "", toString(c(1:10)))
Patrick
  • 742
  • 7
  • 19
  • 9
    This is a dangerous answer -- if there are comma-space sequences in the elements of the vector, this answer will remove them. – C8H10N4O2 Mar 18 '16 at 21:08
5

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="")) 
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
2

Another way would be to use glue package:

glue_collapse(glue("{sdata}"))
paste(glue("{sdata}"), collapse = '')
AlexB
  • 3,061
  • 2
  • 17
  • 19
1

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"
fan
  • 11
  • 6