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
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
Weiwei Shi wrote:> 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"t1 <- tapply(unlist(t0),unlist(t0),function(x){names(x)}) works for your example. Not clear how ``general'' an answer you want. If the entries of your list are not scalars, then the problem is much harder I think. (And may not even be well posed.) But then you should be using a *vector*, not a list. Don't use a chainsaw when you need a fretsaw. cheers, Rolf Turner rolf at math.unb.ca
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?
thanks. split() is what I needed. On 4/11/07, jim holtman <jholtman at gmail.com> wrote:> 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? >-- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III