[R] how to reverse a list

jim holtman jholtman at gmail.com
Thu Apr 12 00:09:14 CEST 2007


try this:

> x <- cbind(unlist(t0), names(t0))
> x
  [,1] [,2]
a "1"  "a"
b "1"  "b"
c "2"  "c"
d "1"  "d"
> split(x[,2], x[,1])
$`1`
  a   b   d
"a" "b" "d"

$`2`
  c
"c"



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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list