

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]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Try this:
> A1<list(c(1:4),c(2,4,5),23,c(4,5,13))
>
> # unlist with the list number
> result < do.call(rbind, sapply(seq(length(A1)), function(.indx){
+ cbind(value = A1[[.indx]], index = .indx)
+ }))
>
> ans < split(result[, 2], result[, 1])
> ans
$`1`
[1] 1
$`2`
[1] 1 2
$`3`
[1] 1
$`4`
[1] 1 2 4
$`5`
[1] 2 4
$`13`
[1] 4
$`23`
[1] 3
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Tue, Jul 8, 2014 at 12:39 PM, Lorenzo Alfieri < [hidden email]>
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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Here is one approach that gives almost the same answer as your example:
> A1<list(c(1:4),c(2,4,5),23,c(4,5,13))
>
> A2 < sort(unique(unlist(A1)))
> names(A2) < A2
> sapply(A2, function(x) which( sapply(A1, function(y) x %in% y) ),
+ simplify=FALSE, USE.NAMES=TRUE )
$`1`
[1] 1
$`2`
[1] 1 2
$`3`
[1] 1
$`4`
[1] 1 2 4
$`5`
[1] 2 4
$`13`
[1] 4
$`23`
[1] 3
If you want the `23` to be in the 23rd element of the list (with empty
values before it) then just change A2 to be a vector from 1 to the
largest value
On Tue, Jul 8, 2014 at 10:39 AM, Lorenzo Alfieri < [hidden email]> 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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.

Gregory (Greg) L. Snow Ph.D.
[hidden email]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


And here is another approach:
> out < vector('list',length(unique(unlist(A1))))
> names(out) < sort(unique(unlist(A1)))
> for( i in seq_along(A1) ) {
+ for( j in as.character(A1[[i]]) ) {
+ out[[j]] < c(out[[j]], i)
+ }
+ }
>
> out
$`1`
[1] 1
$`2`
[1] 1 2
$`3`
[1] 1
$`4`
[1] 1 2 4
$`5`
[1] 2 4
$`13`
[1] 4
$`23`
[1] 3
Which approach is faster/more efficient could depend on what your real
data looks like, how big the list is, how large/variable the vectors
within the list are. Both methods could probably also be improved,
but unless the list is big enough that either takes quite a bit of
time then it is probably not worth the time to optimize them.
On Tue, Jul 8, 2014 at 11:11 AM, Greg Snow < [hidden email]> wrote:
> Here is one approach that gives almost the same answer as your example:
>
>> A1<list(c(1:4),c(2,4,5),23,c(4,5,13))
>>
>> A2 < sort(unique(unlist(A1)))
>> names(A2) < A2
>> sapply(A2, function(x) which( sapply(A1, function(y) x %in% y) ),
> + simplify=FALSE, USE.NAMES=TRUE )
> $`1`
> [1] 1
>
> $`2`
> [1] 1 2
>
> $`3`
> [1] 1
>
> $`4`
> [1] 1 2 4
>
> $`5`
> [1] 2 4
>
> $`13`
> [1] 4
>
> $`23`
> [1] 3
>
> If you want the `23` to be in the 23rd element of the list (with empty
> values before it) then just change A2 to be a vector from 1 to the
> largest value
>
> On Tue, Jul 8, 2014 at 10:39 AM, Lorenzo Alfieri < [hidden email]> 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]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>
>
>
> 
> Gregory (Greg) L. Snow Ph.D.
> [hidden email]

Gregory (Greg) L. Snow Ph.D.
[hidden email]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Here is another approach inspired by Jim's answer:
> names(A1) < paste0(seq_along(A1),'.')
> tmp < unlist(A1)
> split( rep( seq_along(A1), sapply(A1,length) ), as.numeric(sub('\\..+$','',tmp)) )
$`1`
[1] 1
$`2`
[1] 1 2
$`3`
[1] 1
$`4`
[1] 1 2 4
$`5`
[1] 2 4
$`13`
[1] 4
$`23`
[1] 3
On Tue, Jul 8, 2014 at 11:10 AM, jim holtman < [hidden email]> wrote:
> Try this:
>
>> A1<list(c(1:4),c(2,4,5),23,c(4,5,13))
>>
>> # unlist with the list number
>> result < do.call(rbind, sapply(seq(length(A1)), function(.indx){
> + cbind(value = A1[[.indx]], index = .indx)
> + }))
>>
>> ans < split(result[, 2], result[, 1])
>> ans
> $`1`
> [1] 1
>
> $`2`
> [1] 1 2
>
> $`3`
> [1] 1
>
> $`4`
> [1] 1 2 4
>
> $`5`
> [1] 2 4
>
> $`13`
> [1] 4
>
> $`23`
> [1] 3
>
>
>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
>
> On Tue, Jul 8, 2014 at 12:39 PM, Lorenzo Alfieri < [hidden email]>
> 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]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide
>> http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.

Gregory (Greg) L. Snow Ph.D.
[hidden email]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


Oops, I combined 2 ideas (by chance it still worked), the last line
should have been one of the following:
split( rep( seq_along(A1), sapply(A1,length) ), tmp )
split( as.numeric(sub('\\..*$','',names(tmp))), tmp )
On Tue, Jul 8, 2014 at 11:41 AM, Greg Snow < [hidden email]> wrote:
> Here is another approach inspired by Jim's answer:
>
>> names(A1) < paste0(seq_along(A1),'.')
>> tmp < unlist(A1)
>> split( rep( seq_along(A1), sapply(A1,length) ), as.numeric(sub('\\..+$','',tmp)) )
> $`1`
> [1] 1
>
> $`2`
> [1] 1 2
>
> $`3`
> [1] 1
>
> $`4`
> [1] 1 2 4
>
> $`5`
> [1] 2 4
>
> $`13`
> [1] 4
>
> $`23`
> [1] 3
>
> On Tue, Jul 8, 2014 at 11:10 AM, jim holtman < [hidden email]> wrote:
>> Try this:
>>
>>> A1<list(c(1:4),c(2,4,5),23,c(4,5,13))
>>>
>>> # unlist with the list number
>>> result < do.call(rbind, sapply(seq(length(A1)), function(.indx){
>> + cbind(value = A1[[.indx]], index = .indx)
>> + }))
>>>
>>> ans < split(result[, 2], result[, 1])
>>> ans
>> $`1`
>> [1] 1
>>
>> $`2`
>> [1] 1 2
>>
>> $`3`
>> [1] 1
>>
>> $`4`
>> [1] 1 2 4
>>
>> $`5`
>> [1] 2 4
>>
>> $`13`
>> [1] 4
>>
>> $`23`
>> [1] 3
>>
>>
>>
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
>> Tell me what you want to do, not how you want to do it.
>>
>>
>> On Tue, Jul 8, 2014 at 12:39 PM, Lorenzo Alfieri < [hidden email]>
>> 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]]
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/rhelp>>> PLEASE do read the posting guide
>>> http://www.Rproject.org/postingguide.html>>> and provide commented, minimal, selfcontained, reproducible code.
>>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>
>
>
> 
> Gregory (Greg) L. Snow Ph.D.
> [hidden email]

Gregory (Greg) L. Snow Ph.D.
[hidden email]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 < [hidden email]> 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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


You may also try:
library(reshape2)
A2 < melt(A1)
split(A2[,2],A2[,1])
A.K.
On Tuesday, July 8, 2014 12:57 PM, Lorenzo Alfieri < [hidden email]> 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]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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: [hidden email]
> Date: Tue, 8 Jul 2014 12:11:09 0700
> Subject: Re: [R] reorder a list
> To: [hidden email]
> CC: [hidden email]
>
> 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 < [hidden email]> 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]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/rhelp> > PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> > and provide commented, minimal, selfcontained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 < [hidden email]>
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: [hidden email]
> > Date: Tue, 8 Jul 2014 12:11:09 0700
> > Subject: Re: [R] reorder a list
> > To: [hidden email]
> > CC: [hidden email]
>
> >
> > 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 < [hidden email]>
> 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]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/rhelp> > > PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> > > and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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: [hidden email]
Date: Wed, 9 Jul 2014 09:26:14 0700
Subject: Re: [R] reorder a list
To: [hidden email]
CC: [hidden email]
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 < [hidden email]> 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: [hidden email]
> Date: Tue, 8 Jul 2014 12:11:09 0700
> Subject: Re: [R] reorder a list
> To: [hidden email]
> CC: [hidden email]
>
> 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 < [hidden email]> 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]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/rhelp> > PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> > and provide commented, minimal, selfcontained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

