Hi. Can anyone suggest a simple way to re-sort in R a list of vectors of the following form? input $"1" a b c 1 2 3 $"2" a b c 4 5 6 Output should be something like: "a" "1" 1 "2" 4 "b" "1" 2 "2" 5 "c" "1" 3 "2" 6 I've been futzing with mapply(), outer(), split(), rbind() and so on but haven't found an elegant solution. Thanks, Jan. P.S. E-mailed CCs of posted replies appreciated.
If all vectors in the list have the same length, why not use a matrix? Then you'd just transpose the matrix if you need to. If you really have to have it as a list, here's one possibility:> x <- list("1"=c(a=1, b=2, c=3), "2"=c(a=4, b=5, c=6)) > x$"1" a b c 1 2 3 $"2" a b c 4 5 6> as.list(as.data.frame(t(matrix(unlist(x), nrow=3))))$V1 [1] 1 4 $V2 [1] 2 5 $V3 [1] 3 6 Andy> From: Jan Hummel > > Hi. > Can anyone suggest a simple way to re-sort in R a list of > vectors of the > following form? > > input > $"1" > a b c > 1 2 3 > $"2" > a b c > 4 5 6 > > Output should be something like: > "a" > "1" 1 > "2" 4 > "b" > "1" 2 > "2" 5 > "c" > "1" 3 > "2" 6 > > I've been futzing with mapply(), outer(), split(), rbind() > and so on but > haven't found an elegant solution. > > Thanks, > Jan. > > P.S. E-mailed CCs of posted replies appreciated. > > ______________________________________________ > 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 > > >
Thanks a lot! But unfortunately I will not know the dimensions of both lists. And further, the lists may be (partly) disjoint as: x <- list("1"=c(a=1, b=2, c=3), "2"=c(d=4, b=5, e=6)). And last but not least I'm really have to have access to the names of the named list items. The problem I dealt with is in unlist() merging the names together, as you can see in your example given: "V1", "V2" and "V3". Because off interpreting the names later as identifiers in db queries I'm really interested in getting something like list("a"=c("1"=1), "b"=c("1"=2, "2"=5), "c"=c("1"=3), "d"=c("1"=4), "e"=c("1"=6)) for the above input. By giving the result this way I'm able to extract both names from two sets as well as the according value between both items. One point could be to build a matrix but this matrix would have many NA's. So I prefer Lists of Lists. Any ideas? cheers Jan -----Urspr??ngliche Nachricht----- Von: Liaw, Andy [mailto:andy_liaw at merck.com] Gesendet: Montag, 15. August 2005 17:31 An: Jan Hummel; r-help at stat.math.ethz.ch Betreff: RE: [R] Re-sort list of vectors If all vectors in the list have the same length, why not use a matrix? Then you'd just transpose the matrix if you need to. If you really have to have it as a list, here's one possibility:> x <- list("1"=c(a=1, b=2, c=3), "2"=c(a=4, b=5, c=6)) x$"1" a b c 1 2 3 $"2" a b c 4 5 6> as.list(as.data.frame(t(matrix(unlist(x), nrow=3))))$V1 [1] 1 4 $V2 [1] 2 5 $V3 [1] 3 6 Andy> From: Jan Hummel > > Hi. > Can anyone suggest a simple way to re-sort in R a list of vectors of > the following form? > > input > $"1" > a b c > 1 2 3 > $"2" > a b c > 4 5 6 > > Output should be something like: > "a" > "1" 1 > "2" 4 > "b" > "1" 2 > "2" 5 > "c" > "1" 3 > "2" 6 > > I've been futzing with mapply(), outer(), split(), rbind() and so on > but haven't found an elegant solution. > > Thanks, > Jan. > > P.S. E-mailed CCs of posted replies appreciated. > > ______________________________________________ > 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 > > >
You could try using one of the sparse representations of matrices in the SparseM or Matrix packages. Both packages have vignettes. Andy> From: Jan Hummel > > Thanks a lot! But unfortunately I will not know the > dimensions of both lists. And further, the lists may be > (partly) disjoint as: x <- list("1"=c(a=1, b=2, c=3), > "2"=c(d=4, b=5, e=6)). And last but not least I'm really have > to have access to the names of the named list items. > > The problem I dealt with is in unlist() merging the names > together, as you can see in your example given: "V1", "V2" > and "V3". Because off interpreting the names later as > identifiers in db queries I'm really interested in getting > something like list("a"=c("1"=1), "b"=c("1"=2, "2"=5), > "c"=c("1"=3), "d"=c("1"=4), "e"=c("1"=6)) for the above input. > By giving the result this way I'm able to extract both names > from two sets as well as the according value between both items. > > One point could be to build a matrix but this matrix would > have many NA's. So I prefer Lists of Lists. > > Any ideas? > > cheers > Jan > > -----Urspr??ngliche Nachricht----- > Von: Liaw, Andy [mailto:andy_liaw at merck.com] > Gesendet: Montag, 15. August 2005 17:31 > An: Jan Hummel; r-help at stat.math.ethz.ch > Betreff: RE: [R] Re-sort list of vectors > > If all vectors in the list have the same length, why not use > a matrix? Then you'd just transpose the matrix if you need > to. If you really have to have it as a list, here's one possibility: > > > x <- list("1"=c(a=1, b=2, c=3), "2"=c(a=4, b=5, c=6)) x > $"1" > a b c > 1 2 3 > > $"2" > a b c > 4 5 6 > > as.list(as.data.frame(t(matrix(unlist(x), nrow=3)))) > $V1 > [1] 1 4 > > $V2 > [1] 2 5 > > $V3 > [1] 3 6 > > Andy > > > > From: Jan Hummel > > > > Hi. > > Can anyone suggest a simple way to re-sort in R a list of > vectors of > > the following form? > > > > input > > $"1" > > a b c > > 1 2 3 > > $"2" > > a b c > > 4 5 6 > > > > Output should be something like: > > "a" > > "1" 1 > > "2" 4 > > "b" > > "1" 2 > > "2" 5 > > "c" > > "1" 3 > > "2" 6 > > > > I've been futzing with mapply(), outer(), split(), rbind() > and so on > > but haven't found an elegant solution. > > > > Thanks, > > Jan. > > > > P.S. E-mailed CCs of posted replies appreciated. > > > > ______________________________________________ > > 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 > > > > > > > > ______________________________________________ > 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 > > >
Dear list members, thank all of you for replying to my question. Finally I came up with 3 working solutions, which I ask you to rank now in terms of used resources, computation speed, "design questions" etc. x <- list("1" = c(a = 1, b = 2, c = 3), "2" = c(d = 4, b = 5, e = 6)) #looping; Thanks to Jim Holtman solution1<-function(x){ Result <- list() for (i in names(x)){ for (j in names(x[[i]])){ Result[[j]][[i]] <- x[[i]][[j]] } } return(Result) } #lapplying function within function; derived from Solution1 solution2<-function(x){ temporaryList <- list() lapply(names(x),function(y){lapply(names(x[[y]]),function(z){temporaryLi st[[z]][[y]]<<-x[[y]][[z]]})}) return(temporaryList) } #vectorized; Thanks to Dimitris Rizopoulos solution3<-function(x){ y <- data.frame(name = rep(names(x), sapply(x, length)), value unlist(x)) lapply(split(y, unlist(lapply(x, names))), function(z){ res <- z$value; names(res) <- z$name; return(res) }) } I would prefer solution2(), because ... I don't know. Thanks in advance! Jan