Quantcast

How to get all list item to one string variable

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

How to get all list item to one string variable

purushothaman
This post was updated on .
Hi,

How to get all list item to one string variable.

example

a[1]="abc"
a[2]="def"

output="abc def"

Thanks
B.Purushothaman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

Sarah Goslee
With paste().

On Thursday, July 12, 2012, purushothaman wrote:

> Hi,
>
> How to get all list item to one string variable.
>
> example
>
> a[1]="abc"
> b[1]="def"
>
> output="abc def"
>
> Thanks
> B.Purushothaman
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/How-to-get-all-list-item-to-one-string-variable-tp4636283.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] <javascript:;> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


--
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

purushothaman
hi,

sorry it's not 2 different list all item in same list like this

a[1]="abc"
a[2]="def"
...
output ="abc def ..."

Thanks
B.Purushothaman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

arun kirshna
Hi,

If I understand it correctly, probably this is what you want:
a<-list("abc","def","ghi")
 a1<-unlist(a)
 paste(a1[1],a1[2],a1[3],sep=" ")
[1] "abc def ghi"


A.K.



----- Original Message -----
From: purushothaman <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, July 12, 2012 8:22 AM
Subject: Re: [R] How to get all list item to one string variable

hi,

sorry it's not 2 different list all item in same list like this

a[1]="abc"
a[2]="def"
...
output ="abc def ..."

Thanks
B.Purushothaman

--
View this message in context: http://r.789695.n4.nabble.com/How-to-get-all-list-item-to-one-string-variable-tp4636283p4636287.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

arun.gurubaramurugeshan
In reply to this post by purushothaman
Try this...

a<-c("abc","def","ghi","klm","nop","qrs","tuv","wxyz")
b<-data.frame()
for (i in 1:length(a)){
b<-paste(b,a[i],sep="")
}
print(b)


Thanks
Arun
---
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

Ivan Calandra-2
Hi,

What you did is equivalent to, but more complicated (and slower as well
I guess) than:
paste(a, collapse="")

Ivan

--
Ivan CALANDRA
Université de Bourgogne
UMR CNRS/uB 6282 Biogéosciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
[hidden email]
http://biogeosciences.u-bourgogne.fr/calandra



Le 12/07/12 14:54, arun.gurubaramurugeshan a écrit :

Try this...

a<-c("abc","def","ghi","klm","nop","qrs","tuv","wxyz")
b<-data.frame()
for (i in 1:length(a)){
b<-paste(b,a[i],sep="")
}
print(b)


Thanks
Arun
---

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

Petr Savicky
In reply to this post by purushothaman
On Thu, Jul 12, 2012 at 05:22:45AM -0700, purushothaman wrote:
> hi,
>
> sorry it's not 2 different list all item in same list like this
>
> a[1]="abc"
> a[2]="def"
> ...
> output ="abc def ..."

Hi.

Try this

  a <- list("abc", "def", "ghi")
  paste(a, collapse=" ")

  [1] "abc def ghi"

Hope this helps.

Petr Savicky.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

arun kirshna
In reply to this post by purushothaman
HI,
Much more simplified code that my previous one.


 a<-list("abc","def","ghi", "jkl", "mno", "pqr")
 paste(a[],sep=" ")
[1] "abc" "def" "ghi" "jkl" "mno" "pqr"


A.K.



----- Original Message -----
From: purushothaman <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, July 12, 2012 8:22 AM
Subject: Re: [R] How to get all list item to one string variable

hi,

sorry it's not 2 different list all item in same list like this

a[1]="abc"
a[2]="def"
...
output ="abc def ..."

Thanks
B.Purushothaman

--
View this message in context: http://r.789695.n4.nabble.com/How-to-get-all-list-item-to-one-string-variable-tp4636283p4636287.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: How to get all list item to one string variable

MacQueen, Don
In reply to this post by purushothaman
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]]
[1] "abc"

[[2]]
[1] "def"

[[3]]
[1] 3 4 5 6

[[4]]
[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
925-423-1062





On 7/12/12 5:22 AM, "purushothaman" <[hidden email]> wrote:

>hi,
>
>sorry it's not 2 different list all item in same list like this
>
>a[1]="abc"
>a[2]="def"
>...
>output ="abc def ..."
>
>Thanks
>B.Purushothaman
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/How-to-get-all-list-item-to-one-string-varia
>ble-tp4636283p4636287.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...