Dear list, I have a list, as follows : a <- 5 names(a) <- "a" b <- 9 names(b) <- "b" c <- 15 names(c) <- "c" x <- list("i" = a, "j" = b, "j" = c) I want to invert the list, like this : $a i 5 $b j k 9 15 I do not find a clean solution. Could anyone give me elegant ideas ? Thanks in advance, Carlos
Here is one way: xst <- stack(x) let <- letters[cumsum(duplicated(match(xst$ind, letters))) + match(xst$ind, letters)] with(xst, structure(split(structure(values, names = let), ind), .Names row.names(xst)[1:length(unique(ind))])) On Tue, Aug 10, 2010 at 1:58 PM, Carlos Petti <carlos.petti@gmail.com>wrote:> Dear list, > > I have a list, as follows : > > a <- 5 > names(a) <- "a" > b <- 9 > names(b) <- "b" > c <- 15 > names(c) <- "c" > x <- list("i" = a, "j" = b, "j" = c) > > I want to invert the list, like this : > > $a > i > 5 > > $b > j k > 9 15 > > I do not find a clean solution. > > Could anyone give me elegant ideas ? > > Thanks in advance, > Carlos > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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.
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. >
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. >> >
Or rather : n <- sapply(x, function(i) names(i)) tapply(x, n, names) 2010/8/11 Carlos Petti <carlos.petti at gmail.com>:> 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. >>> >> >
Do not worry, I found the solution. I had a list, as follows : x <- list() x$i <- 5 x$j <- 9 x$k <- 15 names(x$i) <- "a" names(x$j) <- "b" names(x$k) <- "b" And I wanted to obtain a list, as follows : $a i 5 $b j k 9 15 My solution : n <- sapply(x, names) m <- sapply(x, c, use.names = FALSE) tapply(m, n, c) Thanks for answers. Carlos 2010/8/11 Wu Gong <wg2f at mtmail.mtsu.edu>:> Hi Carlos, > > I don't know what kind of data you really have. It's would be helpful if you give a sample data. You really don't need to use sapply. > > R can assign names through vectors: > > names(y) <- names(x) > > Or: > > names(y) <- c("NameA","NameB") > > Regards, > > Wu > > > > ________________________________________ > From: Carlos Petti [carlos.petti at gmail.com] > Sent: Wednesday, August 11, 2010 5:39 AM > To: Wu Gong > Cc: r-help at r-project.org > Subject: Re: [R] How to invert a list ? > > Or rather : > n <- sapply(x, function(i) names(i)) > tapply(x, n, names) > > 2010/8/11 Carlos Petti <carlos.petti at gmail.com>: >> 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. >>>> >>> >> >