Dear all
How does one convert a "non-symmetric" list to a vector? See below: > x <- list() > x[[1]] <- letters[1:5] > x[[2]] <- letters[6:10] > x[[3]] <- letters[11:12] > x [[1]] [1] "a" "b" "c" "d" "e" [[2]] [1] "f" "g" "h" "i" "j" [[3]] [1] "k" "l" > paste(x) [1] "c(\"a\", \"b\", \"c\", \"d\", \"e\")" "c(\"f\", \"g\", \"h\", \"i\", \"j\")" [3] "c(\"k\", \"l\")" > as.vector(x) [[1]] [1] "a" "b" "c" "d" "e" [[2]] [1] "f" "g" "h" "i" "j" [[3]] [1] "k" "l" > simplify2array(x) [[1]] [1] "a" "b" "c" "d" "e" [[2]] [1] "f" "g" "h" "i" "j" [[3]] [1] "k" "l" What I would need to get instead is: > letters[1:12] [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" Any ideas? Regards Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail ______________________________________________ [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. |
unlist()
Michael Weylandt On Wed, Aug 10, 2011 at 2:58 PM, Liviu Andronic <[hidden email]>wrote: > Dear all > How does one convert a "non-symmetric" list to a vector? See below: > > > x <- list() > > x[[1]] <- letters[1:5] > > x[[2]] <- letters[6:10] > > x[[3]] <- letters[11:12] > > x > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > > > paste(x) > [1] "c(\"a\", \"b\", \"c\", \"d\", \"e\")" "c(\"f\", \"g\", \"h\", > \"i\", \"j\")" > [3] "c(\"k\", \"l\")" > > as.vector(x) > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > > > simplify2array(x) > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > > > What I would need to get instead is: > > letters[1:12] > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" > > Any ideas? Regards > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail > > ______________________________________________ > [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. > [[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. |
In reply to this post by Liviu Andronic
Check function unlist().
Best, Dimitris On 8/10/2011 8:58 PM, Liviu Andronic wrote: > Dear all > How does one convert a "non-symmetric" list to a vector? See below: > >> x<- list() >> x[[1]]<- letters[1:5] >> x[[2]]<- letters[6:10] >> x[[3]]<- letters[11:12] >> x > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > >> paste(x) > [1] "c(\"a\", \"b\", \"c\", \"d\", \"e\")" "c(\"f\", \"g\", \"h\", > \"i\", \"j\")" > [3] "c(\"k\", \"l\")" >> as.vector(x) > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > >> simplify2array(x) > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > > > What I would need to get instead is: >> letters[1:12] > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" > > Any ideas? Regards > Liviu > > -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ ______________________________________________ [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. |
In reply to this post by Liviu Andronic
unlist(x)
[hidden email] wrote on 08/10/2011 01:58:57 PM: > [image removed] > > [R] convert 'list' to 'vector'? > > Liviu Andronic > > to: > > [hidden email] Help > > 08/10/2011 02:02 PM > > Sent by: > > [hidden email] > > Dear all > How does one convert a "non-symmetric" list to a vector? See below: > > > x <- list() > > x[[1]] <- letters[1:5] > > x[[2]] <- letters[6:10] > > x[[3]] <- letters[11:12] > > x > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > > > paste(x) > [1] "c(\"a\", \"b\", \"c\", \"d\", \"e\")" "c(\"f\", \"g\", \"h\", > \"i\", \"j\")" > [3] "c(\"k\", \"l\")" > > as.vector(x) > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > > > simplify2array(x) > [[1]] > [1] "a" "b" "c" "d" "e" > > [[2]] > [1] "f" "g" "h" "i" "j" > > [[3]] > [1] "k" "l" > > > What I would need to get instead is: > > letters[1:12] > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" > > Any ideas? Regards > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > and provide commented, minimal, self-contained, reproducible code. [[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. |
In reply to this post by Michael Weylandt
On Wed, Aug 10, 2011 at 9:02 PM, R. Michael Weylandt
<[hidden email]> wrote: > unlist() > Thanks all! This is perfect, and very R-ish: never where a novice would expect it to be. Cheers Liviu ______________________________________________ [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. |
On Aug 10, 2011, at 3:10 PM, Liviu Andronic wrote: > On Wed, Aug 10, 2011 at 9:02 PM, R. Michael Weylandt > <[hidden email]> wrote: >> unlist() >> > Thanks all! This is perfect, and very R-ish: never where a novice > would expect it to be. Well, since `unlist` is linked in the See Also on the help page for `list`, I can only hope you meant that in complete jest. As the Posting Guide says: "... sometimes `read the manual' is the appropriate response". -- David Winsemius, MD West Hartford, CT ______________________________________________ [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. |
On Wed, Aug 10, 2011 at 9:32 PM, David Winsemius <[hidden email]> wrote:
>> Thanks all! This is perfect, and very R-ish: never where a novice >> would expect it to be. > > Well, since `unlist` is linked in the See Also on the help page for `list`, > I can only hope you meant that in complete jest. > More or less. I would have expected that to transform a 'list' into a 'vector' I should look into 'as.vector' (or its See Also), and I would have never guessed to look for 'unlist'. R documentation is sometimes (often?) hard to parse, and when learning R more often than not you're looking in the wrong place. But yes, it was intended as humour (although I did expect to get grilled). > As the Posting Guide says: "... sometimes `read the manual' is the > appropriate response". > I did, but I was on the wrong track. It actually hasn't occurred to me to check ?list, but See Also in both ?as.vector and ?simplify2array does not link to 'unlist'. Since these are the two places where I turned to in the first place, and I have also played extensively with sapply(..., simplify=...) arguments, and there was nothing obvious in their respective See Also, I figured that I did my homework reasonably well. Regards Liviu ______________________________________________ [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. |
On Aug 10, 2011, at 3:58 PM, Liviu Andronic wrote: > On Wed, Aug 10, 2011 at 9:32 PM, David Winsemius <[hidden email] > > wrote: >>> Thanks all! This is perfect, and very R-ish: never where a novice >>> would expect it to be. >> >> Well, since `unlist` is linked in the See Also on the help page for >> `list`, >> I can only hope you meant that in complete jest. >> > More or less. I would have expected that to transform a 'list' into a > 'vector' I should look into 'as.vector' (or its See Also), and I would > have never guessed to look for 'unlist'. I am not sure if ?as.vector would necessarily be the place one would have wanted to stay after reading that vector() could return a list. I would also think that attempting this on your proffered data example would have made you want to search elsewhere: > is.vector(x) [1] TRUE You probably didn't realize that lists _are_ vectors before this. > > R documentation is sometimes (often?) hard to parse, and when learning > R more often than not you're looking in the wrong place. But yes, it > was intended as humour (although I did expect to get grilled). Hopefully this expected invitation to grilling will not result in severe burns. I note that one of the *apply family would have been successful in returning the desired object in conjunction with c() : > rapply(x, c) [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" `rapply` is designed to do a complete traversal of the list structure and its default "how" argument is "unlist". It was linked from the page where you were looking at `simplify2array`. >> As the Posting Guide says: "... sometimes `read the manual' is the >> appropriate response". >> > I did, but I was on the wrong track. It actually hasn't occurred to me > to check ?list, but See Also in both ?as.vector and ?simplify2array > does not link to 'unlist'. Perhaps not immediately but after on hop it does. The link to is.list on ?as.vector goes to the help(list) page. > Since these are the two places where I > turned to in the first place, and I have also played extensively with > sapply(..., simplify=...) arguments, and there was nothing obvious in > their respective See Also, I figured that I did my homework reasonably > well. I take your meaning. Learning to keep straight modes straight (atomic versus recursive) and the various classes is part of the "learning cliff". > > Regards > Liviu David Winsemius, MD West Hartford, CT ______________________________________________ [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. |
On Aug 10, 2011, at 23:39 , David Winsemius wrote: > Hopefully this expected invitation to grilling will not result in severe burns. I note that one of the *apply family would have been successful in returning the desired object in conjunction with c() : > > > rapply(x, c) > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" > > `rapply` is designed to do a complete traversal of the list structure and its default "how" argument is "unlist". It was linked from the page where you were looking at `simplify2array`. Speaking of help pages, a brief look at the arguments for c() might be enlightening. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: [hidden email] Priv: [hidden email] "Døden skal tape!" --- Nordahl Grieg ______________________________________________ [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. |
In reply to this post by David Winsemius
On Wed, Aug 10, 2011 at 11:39 PM, David Winsemius
<[hidden email]> wrote: >> is.vector(x) > [1] TRUE > > You probably didn't realize that lists _are_ vectors before this. > Nice to know. > Hopefully this expected invitation to grilling will not result in severe > burns. I note that one of the *apply family would have been successful in > returning the desired object in conjunction with c() : > >> rapply(x, c) > [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" > > `rapply` is designed to do a complete traversal of the list structure and > its default "how" argument is "unlist". It was linked from the page where > you were looking at `simplify2array`. > > c(x, recursive=T) [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" per Peter's suggestion. Thanks all for all the solutions. Regards Liviu ______________________________________________ [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. |
Free forum by Nabble | Edit this page |