[R] concatenate values in the vector

Erik Iverson eriki at ccbr.umn.edu
Wed Aug 4 00:03:12 CEST 2010



Cristian Montes wrote:
> Ok, sorry, I misinterpreted the question!  Here is the right solution
> 
> x <- c("a", "b", "c", "d", "e")
> z <- x[1]
> 
> for (i in 2:length(x))
>     {
>      z<- paste(z, x[i], sep = "+")
>     }
> 
> print(z)
> 
> [1] "a+b+c+d+e"
> 

Or

paste(x, collapse = '+')

which was the solution that was in the message you originally replied to.



More information about the R-help mailing list