Re: [R] Merge list to list - as matrix

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon 28 Aug 2006 - 21:53:52 EST

Here are two ways:

  1. use indexes:

lapply(seq(along = x), function(i) cbind(x[[i]], y[[i]]))

2. use mapply:

mapply(cbind, x, y, SIMPLIFY = FALSE)

On 8/28/06, Muhammad Subianto <msubianto@gmail.com> wrote:
> Dear all,
>
> I have dataset
> x <- list(matrix(1:20, 5, 4),matrix(1:20, 5, 4),matrix(1:20, 5, 4))
> y <- list(matrix(110:114, 5, 1),matrix(110:114, 5, 1),matrix(110:114, 5, 1))
>
> I need merge x and y as list (y put in last column).
> The result is something like
>
> [[1]]
> [,1] [,2] [,3] [,4] [,5]
> [1,] 1 6 11 16 110
> [2,] 2 7 12 17 111
> [3,] 3 8 13 18 112
> [4,] 4 9 14 19 113
> [5,] 5 10 15 20 114
>
> [[2]]
> [,1] [,2] [,3] [,4] [,5]
> [1,] 1 6 11 16 110
> [2,] 2 7 12 17 111
> [3,] 3 8 13 18 112
> [4,] 4 9 14 19 113
> [5,] 5 10 15 20 114
>
> [[3]]
> [,1] [,2] [,3] [,4] [,5]
> [1,] 1 6 11 16 110
> [2,] 2 7 12 17 111
> [3,] 3 8 13 18 112
> [4,] 4 9 14 19 113
> [5,] 5 10 15 20 114
>
> I have tried
> a <- list(x,y)
> as.data.frame(t(sapply(a, rbind)))
> lapply(a, function(x) matrix(unlist(x), nrow = length(x), byrow = TRUE))
> but I don't know how to fix it.
>
> Regards, Muhammad Subianto
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>



R-help@stat.math.ethz.ch 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. Received on Mon Aug 28 22:06:09 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 29 Aug 2006 - 00:23:03 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.