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 <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.

Reply via email to