[R] transposing a list of vectors

2010-01-15 Thread Michael Friendly
I have a list of vectors, all forced to be the same length: testlist - list( shape=c(0, 0, 2), cell.fill=c(red,blue,green), back.fill=rep(white,3), scale.max=rep(100,3) ) str(testlist) List of 4 $ shape: num [1:3] 0 0 2 $ cell.fill: chr [1:3] red blue green $ back.fill: chr

Re: [R] transposing a list of vectors

2010-01-15 Thread jim holtman
Does this do it: testlist - list( + shape=c(0, 0, 2), + cell.fill=c(red,blue,green), + back.fill=rep(white,3), + scale.max=rep(100,3) + ) wanted - lapply(seq(length(testlist[[1]])), function(x){ + # iterate for each element of the list + result - lapply(names(testlist),

Re: [R] transposing a list of vectors

2010-01-15 Thread jim holtman
fingers too fast; didn't need the enclosing 'list': testlist - list( + shape=c(0, 0, 2), + cell.fill=c(red,blue,green), + back.fill=rep(white,3), + scale.max=rep(100,3) + ) wanted - lapply(seq(length(testlist[[1]])), function(x){ + # iterate for each element of the list +

Re: [R] transposing a list of vectors

2010-01-15 Thread Henrique Dallazuanna
Try this: #'1) But apply converts all to 'character' apply(as.data.frame(testlist), 1, as.list) #2) lapply(split(x - as.data.frame(testlist), 1:nrow(x)), as.list) On Fri, Jan 15, 2010 at 11:09 AM, Michael Friendly frien...@yorku.ca wrote: I have a list of vectors, all forced to be the same

Re: [R] transposing a list of vectors

2010-01-15 Thread Gabor Grothendieck
This gives the result you asked for: DF - as.data.frame(testlist) lapply(split(DF, 1:nrow(DF)), unclass) although it might be good enough to just do this depending on what you need: DF - as.data.frame(testlist) split(DF, 1:nrow(DF) On Fri, Jan 15, 2010 at 8:09 AM, Michael Friendly

Re: [R] transposing a list of vectors

2010-01-15 Thread Michael Friendly
Thanks, Henrique I *do* need to preserve the type of each list element (num or chr). Thus, a first step might be as.data.frame(testlist, stringsAsFactors=FALSE) shape cell.fill back.fill scale.max 1 0 red white 100 2 0 blue white 100 3 2 green