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 [1:3] "white" "white" "white" $ scale.max: num [1:3] 100 100 100 > I need to 'transpose' them into a list of lists with named values like so: wanted <- list( list(shape=0, cell.fill="red", back.fill="white", scale.max=100), list(shape=0, cell.fill="blue", back.fill="white", scale.max=100), list(shape=2, cell.fill="green", back.fill="white", scale.max=100) ) > str(wanted) List of 3 $ :List of 4 ..$ shape : num 0 ..$ cell.fill: chr "red" ..$ back.fill: chr "white" ..$ scale.max: num 3 $ :List of 4 ..$ shape : num 0 ..$ cell.fill: chr "blue" ..$ back.fill: chr "white" ..$ scale.max: num 3 $ :List of 4 ..$ shape : num 2 ..$ cell.fill: chr "green" ..$ back.fill: chr "white" ..$ scale.max: num 3 > How can I do this in general? -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html Toronto, ONT M3J 1P3 CANADA
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), function(.name){ + list(testlist[[.name]][x]) + }) + names(result) <- names(testlist) + result + })> dput(wanted)list(structure(list(shape = list(0), cell.fill = list("red"), back.fill = list("white"), scale.max = list(100)), .Names = c("shape", "cell.fill", "back.fill", "scale.max")), structure(list(shape = list( 0), cell.fill = list("blue"), back.fill = list("white"), scale.max = list(100)), .Names = c("shape", "cell.fill", "back.fill", "scale.max")), structure(list(shape = list(2), cell.fill list( "green"), back.fill = list("white"), scale.max = list(100)), .Names c("shape", "cell.fill", "back.fill", "scale.max")))> >On Fri, Jan 15, 2010 at 8:09 AM, Michael Friendly <friendly@yorku.ca> wrote:> 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 [1:3] "white" "white" "white" > $ scale.max: num [1:3] 100 100 100 > > > > I need to 'transpose' them into a list of lists with named values like so: > > wanted <- list( > list(shape=0, cell.fill="red", back.fill="white", scale.max=100), > list(shape=0, cell.fill="blue", back.fill="white", scale.max=100), > list(shape=2, cell.fill="green", back.fill="white", scale.max=100) > ) > > str(wanted) > List of 3 > $ :List of 4 > ..$ shape : num 0 > ..$ cell.fill: chr "red" > ..$ back.fill: chr "white" > ..$ scale.max: num 3 > $ :List of 4 > ..$ shape : num 0 > ..$ cell.fill: chr "blue" > ..$ back.fill: chr "white" > ..$ scale.max: num 3 > $ :List of 4 > ..$ shape : num 2 > ..$ cell.fill: chr "green" > ..$ back.fill: chr "white" > ..$ scale.max: num 3 > > > > How can I do this in general? > > -- > Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology > Dept. > York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 > 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html > Toronto, ONT M3J 1P3 CANADA > > ______________________________________________ > 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<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 that you are trying to solve? [[alternative HTML version deleted]]
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 + result <- lapply(names(testlist), function(.name){ + testlist[[.name]][x] + }) + names(result) <- names(testlist) + result + })> dput(wanted)list(structure(list(shape = 0, cell.fill = "red", back.fill = "white", scale.max = 100), .Names = c("shape", "cell.fill", "back.fill", "scale.max")), structure(list(shape = 0, cell.fill = "blue", back.fill = "white", scale.max = 100), .Names = c("shape", "cell.fill", "back.fill", "scale.max")), structure(list(shape = 2, cell.fill = "green", back.fill = "white", scale.max = 100), .Names c("shape", "cell.fill", "back.fill", "scale.max")))>On Fri, Jan 15, 2010 at 8:09 AM, Michael Friendly <friendly@yorku.ca> wrote:> 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 [1:3] "white" "white" "white" > $ scale.max: num [1:3] 100 100 100 > > > > I need to 'transpose' them into a list of lists with named values like so: > > wanted <- list( > list(shape=0, cell.fill="red", back.fill="white", scale.max=100), > list(shape=0, cell.fill="blue", back.fill="white", scale.max=100), > list(shape=2, cell.fill="green", back.fill="white", scale.max=100) > ) > > str(wanted) > List of 3 > $ :List of 4 > ..$ shape : num 0 > ..$ cell.fill: chr "red" > ..$ back.fill: chr "white" > ..$ scale.max: num 3 > $ :List of 4 > ..$ shape : num 0 > ..$ cell.fill: chr "blue" > ..$ back.fill: chr "white" > ..$ scale.max: num 3 > $ :List of 4 > ..$ shape : num 2 > ..$ cell.fill: chr "green" > ..$ back.fill: chr "white" > ..$ scale.max: num 3 > > > > How can I do this in general? > > -- > Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology > Dept. > York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 > 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html > Toronto, ONT M3J 1P3 CANADA > > ______________________________________________ > 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<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 that you are trying to solve? [[alternative HTML version deleted]]
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 <friendly at yorku.ca> wrote:> 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 [1:3] "white" "white" "white" > $ scale.max: num [1:3] 100 100 100 >> > > I need to 'transpose' them into a list of lists with named values like so: > > wanted <- list( > ? list(shape=0, cell.fill="red", back.fill="white", scale.max=100), > ? list(shape=0, cell.fill="blue", back.fill="white", scale.max=100), > ? list(shape=2, cell.fill="green", back.fill="white", scale.max=100) > ? ) >> str(wanted) > List of 3 > $ :List of 4 > ?..$ shape ? ?: num 0 > ?..$ cell.fill: chr "red" > ?..$ back.fill: chr "white" > ?..$ scale.max: num 3 > $ :List of 4 > ?..$ shape ? ?: num 0 > ?..$ cell.fill: chr "blue" > ?..$ back.fill: chr "white" > ?..$ scale.max: num 3 > $ :List of 4 > ?..$ shape ? ?: num 2 > ?..$ cell.fill: chr "green" > ?..$ back.fill: chr "white" > ?..$ scale.max: num 3 >> > > How can I do this in general? > > -- > Michael Friendly ? ? Email: friendly AT yorku DOT ca Professor, Psychology > Dept. > York University ? ? ?Voice: 416 736-5115 x66249 Fax: 416 736-5814 > 4700 Keele Street ? ?http://www.math.yorku.ca/SCS/friendly.html > Toronto, ONT ?M3J 1P3 CANADA > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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 <friendly at yorku.ca> wrote:> 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 [1:3] "white" "white" "white" > $ scale.max: num [1:3] 100 100 100 >> > > I need to 'transpose' them into a list of lists with named values like so: > > wanted <- list( > ? list(shape=0, cell.fill="red", back.fill="white", scale.max=100), > ? list(shape=0, cell.fill="blue", back.fill="white", scale.max=100), > ? list(shape=2, cell.fill="green", back.fill="white", scale.max=100) > ? ) >> str(wanted) > List of 3 > $ :List of 4 > ?..$ shape ? ?: num 0 > ?..$ cell.fill: chr "red" > ?..$ back.fill: chr "white" > ?..$ scale.max: num 3 > $ :List of 4 > ?..$ shape ? ?: num 0 > ?..$ cell.fill: chr "blue" > ?..$ back.fill: chr "white" > ?..$ scale.max: num 3 > $ :List of 4 > ?..$ shape ? ?: num 2 > ?..$ cell.fill: chr "green" > ?..$ back.fill: chr "white" > ?..$ scale.max: num 3 >> > > How can I do this in general? > > -- > Michael Friendly ? ? Email: friendly AT yorku DOT ca Professor, Psychology > Dept. > York University ? ? ?Voice: 416 736-5115 x66249 Fax: 416 736-5814 > 4700 Keele Street ? ?http://www.math.yorku.ca/SCS/friendly.html > Toronto, ONT ?M3J 1P3 CANADA > > ______________________________________________ > 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. >