[R] How to get all list item to one string variable

MacQueen, Don macqueen1 at llnl.gov
Fri Jul 13 18:51:04 CEST 2012

First of all, although the question uses the term "list", the object named
'a' is clearly not a list as R defines the term. It is a vector.

Thus a better example answer is

  a <- c('abc', 'def')
  paste(a , collapse=' ')

and this works for however many elements there are in the vector a.

The example
  a <- list("abc", "def", "ghi")
  paste(a, collapse=" ")
works, but only because the paste() function first converts its arguments
to character strings.

I only mention this because it's important (especially for those new to R)
to understand the distinction between lists and vectors, even if they
occasionally appear to behave the same way. Consider this:

>   a <- list("abc", "def", 3:6,"ghi")

> a
[1] "abc"

[1] "def"

[1] 3 4 5 6

[1] "ghi"

>   paste(a, collapse=" ")
[1] "abc def 3:6 ghi"

And I did not expect that result, but something like this:

> unlist(a)
[1] "abc" "def" "3"   "4"   "5"   "6"   "ghi"

> paste(unlist(a), collapse=' ')
[1] "abc def 3 4 5 6 ghi"


Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

On 7/12/12 5:22 AM, "purushothaman" <purushothaman.b at ge.com> wrote:

>sorry it's not 2 different list all item in same list like this
>output ="abc def ..."
>View this message in context:
>Sent from the R help mailing list archive at Nabble.com.
>R-help at r-project.org mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list