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 <-
lappl
9815 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 e
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.co
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 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
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
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 <538...@gmail.com> wrote:
> Here is an
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 T
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]
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`
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
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
11 matches
Mail list logo