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-boun...@r-project.org> On Behalf Of Rolf Turner Sent: Thursday, September 26, 2024 11:56 PM To: r-help@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@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. ______________________________________________ R-help@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.