Untitled

By: a guest on Aug 6th, 2012  |  syntax: None  |  size: 1.29 KB  |  hits: 12  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. Combine lists in a list
2. \$`1`
3. [1] 1959   13
4.
5. \$`2`
6. [1] 2280  178   13
7.
8. \$`3`
9. [1] 2612  178   13
10.
11. \$`4`
12. [1] 2902  178   13
13.
14. structure(list(`1` = c(1959, 13), `2` = c(2280, 178, 13), `3` = c(2612,
15. 178, 13), `4` = c(2902, 178, 13)......,.Names = c("1", "2", "3", "4"....)
16.
17. \$`list`
18. [1] 1959   13
19. [2] 2280  178   13
20. [3] 2612  178   13
21. [4] 2902  178   13
22.
23. LIST <- structure(list(`1` = c(1959, 13), `2` = c(2280, 178, 13), `3` = c(2612,
24. 178, 13), `4` = c(2902, 178, 13)))
25.
26. cbind.fill <-
27. function(...) {
28.     nm <- list(...)
29.     nm<-lapply(nm, as.matrix)
30.     n <- max(sapply(nm, nrow))
31.     do.call(cbind, lapply(nm, function(x) rbind(x,
32.         matrix(, n - nrow(x), ncol(x)))))
33. }
34.
35. t(do.call('cbind.fill', LIST))
36. print(X\$list, na.print="", quote=FALSE)
37.
38. LIST <- lapply(LIST, function(x) data.frame(t(x)))
39. library(plyr)
40. rbind.fill(LIST)
41.
42. names(LIST) <- NULL
43. LIST
44.
45. x <- list()
46. x\$`1` <- c(1959, 13)
47. x\$`2` <- c(2280, 178, 13)
48. x\$`3` <- c(2612, 178, 13)
49. x\$`4` <- c(2902, 178, 13)
50.
51. # maximum number of elements in any vector
52. max <- max(sapply(x, function(y) length(y)))
53.
54. # make all vectors the same length
55. x <- lapply(x, function (y){length(y) <- max; y})
56.
57. # combine them in a matrix
58. result <- do.call(rbind, x)
59.
60. > result
61.   [,1] [,2] [,3]
62. 1 1959   13   NA
63. 2 2280  178   13
64. 3 2612  178   13
65. 4 2902  178   13