[R] How to invert a list ?

Carlos Petti carlos.petti at gmail.com
Wed Aug 11 12:36:53 CEST 2010


A beginning of solution...

n <- sapply(x, function(i) names(i))
tapply(x, n, c)

2010/8/11 Carlos Petti <carlos.petti at gmail.com>:
> Thanks.
>
> On the other hand,
> I try to obtain the same result but from this list :
>
> x <- list()
> x$i <- 5
> x$j <- 9
> x$k <- 15
> names(x$i) <- "a"
> names(x$j) <- "b"
> names(x$k) <- "b"
>
> Thanks in advance,
> Carlos
>
> 2010/8/10 Wu Gong <wg2f at mtmail.mtsu.edu>:
>>
>> Hi Carlos,
>>
>> I give a handmade code, hope it helps.
>>
>> y <- list()
>> y$a <- a
>> y$b <- c(b,c)
>> names(y$a) <- "i"
>> names(y$b) <- c("j","k")
>>
>>
>> Carlos Petti wrote:
>>>
>>> a <- 5
>>> names(a) <- "a"
>>> b <- 9
>>> names(b) <- "b"
>>> c <- 15
>>> names(c) <- "c"
>>> x <- list("i" = a, "j" = b, "j" = c)
>>>
>>
>>
>> -----
>> A R learner.
>> --
>> View this message in context: http://r.789695.n4.nabble.com/How-to-invert-a-list-tp2320108p2320433.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at 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.
>>
>



More information about the R-help mailing list