
Untitled
By: a guest on
Aug 6th, 2012 | syntax:
None | size: 1.29 KB | hits: 12 | expires: Never
Combine lists in a list
$`1`
[1] 1959 13
$`2`
[1] 2280 178 13
$`3`
[1] 2612 178 13
$`4`
[1] 2902 178 13
structure(list(`1` = c(1959, 13), `2` = c(2280, 178, 13), `3` = c(2612,
178, 13), `4` = c(2902, 178, 13)......,.Names = c("1", "2", "3", "4"....)
$`list`
[1] 1959 13
[2] 2280 178 13
[3] 2612 178 13
[4] 2902 178 13
LIST <- structure(list(`1` = c(1959, 13), `2` = c(2280, 178, 13), `3` = c(2612,
178, 13), `4` = c(2902, 178, 13)))
cbind.fill <-
function(...) {
nm <- list(...)
nm<-lapply(nm, as.matrix)
n <- max(sapply(nm, nrow))
do.call(cbind, lapply(nm, function(x) rbind(x,
matrix(, n - nrow(x), ncol(x)))))
}
t(do.call('cbind.fill', LIST))
print(X$list, na.print="", quote=FALSE)
LIST <- lapply(LIST, function(x) data.frame(t(x)))
library(plyr)
rbind.fill(LIST)
names(LIST) <- NULL
LIST
x <- list()
x$`1` <- c(1959, 13)
x$`2` <- c(2280, 178, 13)
x$`3` <- c(2612, 178, 13)
x$`4` <- c(2902, 178, 13)
# maximum number of elements in any vector
max <- max(sapply(x, function(y) length(y)))
# make all vectors the same length
x <- lapply(x, function (y){length(y) <- max; y})
# combine them in a matrix
result <- do.call(rbind, x)
> result
[,1] [,2] [,3]
1 1959 13 NA
2 2280 178 13
3 2612 178 13
4 2902 178 13