[R] how to reverse a list
Weiwei Shi
helprhelp at gmail.com
Wed Apr 11 23:45:44 CEST 2007
I forgot to add my bad solution here:
reverseList <- function(xlist){
blist <- xlist[!is.na(xlist)]
x0 <- unlist(blist)
l0 <- length(blist)
d0 <- as.data.frame(matrix(0, l0, 3))
d0[,1] <- names(x0)
d0[,2] <- x0
# unique llid
llid <- unique(d0[,2])
outlist <- list( )
for (i in 1:length(llid)) {
each <- llid[i]
probes <- d0[d0[,2]==each, 1]
outlist <- c(outlist, list(probes))
}
names(outlist) <- as.character(llid)
outlist
}
On 4/11/07, Weiwei Shi <helprhelp at gmail.com> wrote:
> Hi, there:
>
> I am wondering if there is a quick way to "reverse" a list like this:
>
> t0 <- list(a=1, b=1, c=2, d=1)
>
> reverst t0 to t1
>
> > t1
> $`1`
> [1] "a" "b" "d"
>
> $`2`
> [1] "c"
>
>
> thanks.
> --
> Weiwei Shi, Ph.D
> Research Scientist
> GeneGO, Inc.
>
> "Did you always know?"
> "No, I did not. But I believed..."
> ---Matrix III
>
--
Weiwei Shi, Ph.D
Research Scientist
GeneGO, Inc.
"Did you always know?"
"No, I did not. But I believed..."
---Matrix III
More information about the R-help
mailing list