Thanks for the suggestion. I found that I get the result I wanted with this simple command:
split(unlist(A2),unlist(A1)) $`1` [1] 2.718282 $`2` [1] 7.389056 7.389056 $`3` [1] 20.08554 $`4` [1] 54.59815 54.59815 54.59815 $`5` [1] 148.4132 148.4132 $`13` [1] 442413.4 $`23` [1] 9744803446 which is indeed very similar to Bill's solution Alfio From: wdun...@tibco.com Date: Wed, 9 Jul 2014 09:26:14 -0700 Subject: Re: [R] reorder a list To: alfio...@hotmail.com CC: r-help@r-project.org Is the following 'g' what you want? A better example might be with A2a <- lapply(A1, function(x)x+seq_along(x)/(100*length(x))) g <- function (x, y) { xLengths <- vapply(x, FUN = length, FUN.VALUE = 0L) yLengths <- vapply(y, FUN = length, FUN.VALUE = 0L) stopifnot(identical(xLengths, yLengths)) split(unlist(y, use.names = FALSE), unlist(x, use.names = FALSE)) }Used as> g(A1,A2)$`1`[1] 2.718282 $`2`[1] 7.389056 7.389056 $`3`[1] 20.08554 $`4`[1] 54.59815 54.59815 54.59815 $`5`[1] 148.4132 148.4132 $`13`[1] 442413.4 $`23`[1] 9744803446 Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Jul 9, 2014 at 2:04 AM, Lorenzo Alfieri <alfio...@hotmail.com> wrote: Thanks Bill and the other guys for the variety of useful replies! In fact I'm working with pretty big lists (with ~35000 sublists) and Bill's solution is the fastest one in terms of computing time. Now comes the second part of the question... :-) I've my usual list of values and time indices to sort: A1<-list(c(1:4),c(2,4,5),23,c(4,5,13)) and then another list A2 with variables which have to be paired with the values of A1: A2<-sapply(A1, "exp") #(in my case there's no exp relation between A1 and A2, they're completely uncorrelated. That's just an example ) > A2 [[1]] [1] 2.718282 7.389056 20.085537 54.598150 [[2]] [1] 7.389056 54.598150 148.413159 [[3]] [1] 9744803446 [[4]] [1] 54.59815 148.41316 442413.39201 Now I'd like to reorder the elements of A2 according to the same rule applied for A1: f <- function (x) { lengths <- vapply(x, FUN = length, FUN.VALUE = 0L) split(rep(seq_along(x), lengths), unlist(x, use.names = FALSE)) } B1<-f(A1) and thus obtain a list B2 which looks like this: > B2 $`1` [1] 2.718282 $`2` [1] 7.389056 7.389056 $`3` [1] 20.08554 $`4` [1] 54.59815 54.59815 54.59815 $`5` [1] 148.4132 148.4132 $`13` [1] 442413.4 $`23` [1] 9744803446 (In this example each element is the exp() of the sublist name, but in a general case they would be uncorrelated, and the resulting elements of each sublist would be different) Any idea? Alfio > From: wdun...@tibco.com > Date: Tue, 8 Jul 2014 12:11:09 -0700 > Subject: Re: [R] reorder a list > To: alfio...@hotmail.com > CC: r-help@r-project.org > > f <- function (x) { > lengths <- vapply(x, FUN = length, FUN.VALUE = 0L) > split(rep(seq_along(x), lengths), unlist(x, use.names = FALSE)) > } > f(A1) # gives about what you want (has, e.g., name 23, not position > 23, in output) > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > > On Tue, Jul 8, 2014 at 9:39 AM, Lorenzo Alfieri <alfio...@hotmail.com> wrote: > > Hi, > > I'm trying to find a way to reorder the elements of a list. > > Let's say I have a list like this: > > A1<-list(c(1:4),c(2,4,5),23,c(4,5,13)) > > > >> A1 > > [[1]] > > [1] 1 2 3 4 > > > > [[2]] > > [1] 2 4 5 > > > > [[3]] > > [1] 23 > > > > [[4]] > > [1] 4 5 13 > > > > All the elements included in it are values, while each sublist is a time > > index > > Now, I'd like to reorder the list (without looping) so to obtain one > > sublist for each value, which include all the time indices where each value > > appears. > > In other words, the result should look like this: > >>A2 > > [[1]] > > [1] 1 > > > > [[2]] > > [1] 1 2 #because value "2" appears in the time index [[1]] and [[2]] of > > A1 > > > > [[3]] > > [1] 1 > > > > [[4]] > > [1] 1 2 4 > > > > [[5]] > > [1] 2 4 > > > > [[13]] > > [1] 4 > > > > [[23]] > > [1] 3 > > > > Any suggestion? > > Thanks > > Alfio > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org 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]] ______________________________________________ R-help@r-project.org 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.