I have (toy example): x <- list(`1` = c(7, 13, 1, 4, 10), `2` = c(2, 5, 14, 8, 11), `3` = c(6, 9, 15, 12, 3)) and f <- factor(rep(1:3,5)) I want to create a vector v of length 15 such that the entries of v, corresponding to level l of f are the entries of x[[l]]. I.e. I want v to equal c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3) I can create v "easily enough", using say, a for-loop. It seems to me, though, that there should be sexier (single command) way of achieving the desired result. However I cannot devise one. Can anyone point me in the right direction? Thanks. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Stats. Dep't. (secretaries) phone: +64-9-373-7599 ext. 89622 Home phone: +64-9-480-4619
Rolf, This works, albeit you may not be thrilled: x <- list(`1` = c(7, 13, 1, 4, 10), `2` = c(2, 5, 14, 8, 11), `3` = c(6, 9, 15, 12, 3)) as.vector(rbind(x[[1]], x[[2]], x[[3]])) -- output:> as.vector(rbind(x[[1]], x[[2]], x[[3]]))[1] 7 2 6 13 5 9 1 14 15 4 8 12 10 11 3 -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Rolf Turner Sent: Thursday, September 26, 2024 11:56 PM To: r-help at r-project.org Subject: [R] Is there a sexy way ...? I have (toy example): x <- list(`1` = c(7, 13, 1, 4, 10), `2` = c(2, 5, 14, 8, 11), `3` = c(6, 9, 15, 12, 3)) and f <- factor(rep(1:3,5)) I want to create a vector v of length 15 such that the entries of v, corresponding to level l of f are the entries of x[[l]]. I.e. I want v to equal c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3) I can create v "easily enough", using say, a for-loop. It seems to me, though, that there should be sexier (single command) way of achieving the desired result. However I cannot devise one. Can anyone point me in the right direction? Thanks. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Stats. Dep't. (secretaries) phone: +64-9-373-7599 ext. 89622 Home phone: +64-9-480-4619 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
How about: as.vector(do.call(rbind,x)) Cheers, Bert However, I much prefer a 2 line version: On Thu, Sep 26, 2024 at 8:56?PM Rolf Turner <rolfturner at posteo.net> wrote:> > I have (toy example): > > x <- list(`1` = c(7, 13, 1, 4, 10), > `2` = c(2, 5, 14, 8, 11), > `3` = c(6, 9, 15, 12, 3)) > and > > f <- factor(rep(1:3,5)) > > I want to create a vector v of length 15 such that the entries of v, > corresponding to level l of f are the entries of x[[l]]. I.e. I want > v to equal > > c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3) > > I can create v "easily enough", using say, a for-loop. It seems to me, > though, that there should be sexier (single command) way of achieving > the desired result. However I cannot devise one. > > Can anyone point me in the right direction? Thanks. > > cheers, > > Rolf Turner > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Stats. Dep't. (secretaries) phone: > +64-9-373-7599 ext. 89622 > Home phone: +64-9-480-4619 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Dear Rolf, dear all, this was an inspiring challenge :-) This seems to do the task... --- snip --- x <- list(`1` = c(7, 13, 1, 4, 10), `2` = c(2, 5, 14, 8, 11), `3` = c(6, 9, 15, 12, 3)) f <- factor(rep(1:3,5)) v <- as.vector(unlist(x)[ paste(rep(levels(f), length(x[[1]])), rep(1:length(x[[1]]), each=length(levels(f))), sep="") ]) v --- snip --- I leave it to you, whether this is an elegant solution or not ;-) Cheers, Kimmo Rolf Turner kirjoitti 27.9.2024 klo 6.55:> > I have (toy example): > > x <- list(`1` = c(7, 13, 1, 4, 10), > `2` = c(2, 5, 14, 8, 11), > `3` = c(6, 9, 15, 12, 3)) > and > > f <- factor(rep(1:3,5)) > > I want to create a vector v of length 15 such that the entries of v, > corresponding to level l of f are the entries of x[[l]]. I.e. I want > v to equal > > c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3) > > I can create v "easily enough", using say, a for-loop. It seems to me, > though, that there should be sexier (single command) way of achieving > the desired result. However I cannot devise one. > > Can anyone point me in the right direction? Thanks. > > cheers, > > Rolf Turner >-- Kimmo Elo Senior Lecturer | Adjunct professor, Dr. =======================================================University of Eastern Finland Department of Geographical and Historical Studies P.O. Box 111 FIN-80101 Joensuu Finland E-mail: kimmo.elo at uef.fi ResearchGate: www.researchgate.net/profile/Kimmo_Elo LAWPOL Consortium (PI): https://lawpol.fi/en ========================================================
On 2024-09-26 11:55 p.m., Rolf Turner wrote:> > I have (toy example): > > x <- list(`1` = c(7, 13, 1, 4, 10), > `2` = c(2, 5, 14, 8, 11), > `3` = c(6, 9, 15, 12, 3)) > and > > f <- factor(rep(1:3,5)) > > I want to create a vector v of length 15 such that the entries of v, > corresponding to level l of f are the entries of x[[l]]. I.e. I want > v to equal > > c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3) > > I can create v "easily enough", using say, a for-loop. It seems to me, > though, that there should be sexier (single command) way of achieving > the desired result. However I cannot devise one. >Don't you find a for loop's naked display of intention to be sexy? Duncan Murdoch
Rolf, I, and many others have come up with an assortment of solutions that seem to work, often by ignoring whatever you intend by mentioning f as a factor. But consider a dumb question. Why are you starting with a list of vectors, with odd pseudo-numeric names? Many of the solutions started by converting your data structure into either a data.frame of some kind or a matrix since all your data seemed numeric. Realistically, your item is pretty much already a data.frame minus the class designation and a list of row names. Once you have a 2-D representation, various methods allow you to rotate it, or read it off a row at a time. But a deeper issue is looking at how you might approach this in other languages such as Python that has a zip functionality. It is very common there to want to iterate over multiple "lists" simultaneously, sort of by giving it multiple columns and having it weaved together. An enumerate version just adds a column of sequence numbers. Your example might look like this IN PYTHON: This is not meant for anything but illustration as something somebody probably has already done in R if you can find some package that supports this, but note the work "zip" is used for compression. And note Python uses a concept of generators so that "zipped" below is a valid generator but to be show must be coerced to run to completion as in asking for a list of it. And it gets consumed after first use so you need to save it as a list version for this exercise. ---- PYTHON CODE ---- x = [ (7, 13, 1, 4, 10), (2, 5, 14, 8, 11), (6, 9, 15, 12, 3) ] zipped = list(zip(x[0], x[1], x[2])) flattened = [num for elem in zipped for num in elem] x zipped flattened ---- OUTPUT --->>> x = [ (7, 13, 1, 4, 10),... (2, 5, 14, 8, 11), ... (6, 9, 15, 12, 3) ... ]>>> zipped = zip(x[0], x[1], x[2]) >>> flattened = [num for elem in zipped for num in elem] >>> x[(7, 13, 1, 4, 10), (2, 5, 14, 8, 11), (6, 9, 15, 12, 3)]>>> zipped<zip object at 0x000001CBF0F43140>>>> flattened[7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3] ---END OUTPUT --- I am not saying to use python, just showing other ways people find comfortable. But choosing the right data structure can make things easy, or at least easier. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Rolf Turner Sent: Thursday, September 26, 2024 11:56 PM To: r-help at r-project.org Subject: [R] Is there a sexy way ...? I have (toy example): x <- list(`1` = c(7, 13, 1, 4, 10), `2` = c(2, 5, 14, 8, 11), `3` = c(6, 9, 15, 12, 3)) and f <- factor(rep(1:3,5)) I want to create a vector v of length 15 such that the entries of v, corresponding to level l of f are the entries of x[[l]]. I.e. I want v to equal c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3) I can create v "easily enough", using say, a for-loop. It seems to me, though, that there should be sexier (single command) way of achieving the desired result. However I cannot devise one. Can anyone point me in the right direction? Thanks. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Stats. Dep't. (secretaries) phone: +64-9-373-7599 ext. 89622 Home phone: +64-9-480-4619 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.