B. Jonathan B. Jonathan
2011-Feb-06 18:32 UTC
[R] Applying 'cbind/rbind' among different list object
Hi, I am wondering whether we can apply 'cbind/rbind' on many **equivalent** list objects. For example please consider following:> list1 <- list2 <- vector("list", length=2); names(list1) <- names(list2)<- c("a", "b")> list1[[1]] <- matrix(1:25, 5) > list1[[2]] <- matrix(2:26, 5) > list2[[1]] <- 10:14 > list2[[2]] <- 11:15 > list1$a [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 $b [,1] [,2] [,3] [,4] [,5] [1,] 2 7 12 17 22 [2,] 3 8 13 18 23 [3,] 4 9 14 19 24 [4,] 5 10 15 20 25 [5,] 6 11 16 21 26> list2$a [1] 10 11 12 13 14 $b [1] 11 12 13 14 15 Here I want to "rbind" these 2 list-s according to "a" and "b" i.e. I want to get another list of length 2 where each element will be matrix with (6x5) dimension. How can I do that? Thanks for your time [[alternative HTML version deleted]]
will this do it for you:> lapply(seq(length(list1)), function(i)rbind(list1[[i]], list2[[i]]))[[1]] [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 [6,] 10 11 12 13 14 [[2]] [,1] [,2] [,3] [,4] [,5] [1,] 2 7 12 17 22 [2,] 3 8 13 18 23 [3,] 4 9 14 19 24 [4,] 5 10 15 20 25 [5,] 6 11 16 21 26 [6,] 11 12 13 14 15 On Sun, Feb 6, 2011 at 1:32 PM, B. Jonathan B. Jonathan <bkheijonathan at gmail.com> wrote:> Hi, I am wondering whether we can apply 'cbind/rbind' on many **equivalent** > list objects. For example please consider following: > >> list1 <- list2 <- vector("list", length=2); names(list1) <- names(list2) > <- c("a", "b") >> list1[[1]] <- matrix(1:25, 5) >> list1[[2]] <- matrix(2:26, 5) >> list2[[1]] <- 10:14 >> list2[[2]] <- 11:15 >> list1 > $a > ? ? [,1] [,2] [,3] [,4] [,5] > [1,] ? ?1 ? ?6 ? 11 ? 16 ? 21 > [2,] ? ?2 ? ?7 ? 12 ? 17 ? 22 > [3,] ? ?3 ? ?8 ? 13 ? 18 ? 23 > [4,] ? ?4 ? ?9 ? 14 ? 19 ? 24 > [5,] ? ?5 ? 10 ? 15 ? 20 ? 25 > $b > ? ? [,1] [,2] [,3] [,4] [,5] > [1,] ? ?2 ? ?7 ? 12 ? 17 ? 22 > [2,] ? ?3 ? ?8 ? 13 ? 18 ? 23 > [3,] ? ?4 ? ?9 ? 14 ? 19 ? 24 > [4,] ? ?5 ? 10 ? 15 ? 20 ? 25 > [5,] ? ?6 ? 11 ? 16 ? 21 ? 26 >> list2 > $a > [1] 10 11 12 13 14 > $b > [1] 11 12 13 14 15 > > Here I want to "rbind" these 2 list-s according to "a" and "b" i.e. I want > to get another list of length 2 where each element will be matrix with (6x5) > dimension. How can I do that? > > Thanks for your time > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?