Saturday, March 7, 2009

Transpose list

transposeList <- function(aList) {
if (length(aList) == 0) {
return(c())
}

colNames <- names(aList[[1]])
result <- replicate(length(colNames), list())
names(result) <- colNames
rowNames <- names(aList)
for (row in rowNames) {
for (col in colNames) {
result[[col]][[row]] = aList[[row]][[col]]
}
}
return(result)
}
What's a better way to do this?