Dear all,
#Last week, I asked about merge x and y as list.
#Now I have a dataset with list of list like:
x <- list(list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)),
list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)))
y <- list(list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1)),
list(c(1, 1, 1, 1, 1),c(1, 1, -1, 1, -1)))
x
y
#I need merge x and y, I have tried with
list.uni <- vector("list", length(x))
for (i in 1:length(x)) {
for (j in 1:length(x[[1]])) {
list.uni[[i]][[j]] <- mapply(cbind,
x[[i]][[j]],
y[[i]][[j]],
SIMPLIFY=FALSE)
}
}
list.uni
I have learn about ?lapply, ?sapply and ?mapply but I still didn't
understand how to use it.
I need the result something like
[[1]]
[[1]][[1]]
[,1] [,2] [,3] [,4] [5]
[1,] 1 6 11 16 1
[2,] 2 7 12 17 -1
[3,] 3 8 13 18 -1
[4,] 4 9 14 19 1
[5,] 5 10 15 20 1
[[1]][[2]]
[,1] [,2] [,3] [,4] [5]
[1,] 1 6 11 16 1
[2,] 2 7 12 17 1
[3,] 3 8 13 18 -1
[4,] 4 9 14 19 -1
[5,] 5 10 15 20 -1
[[2]]
[[2]][[1]]
[,1] [,2] [,3] [,4] [5]
[1,] 1 6 11 16 1
[2,] 2 7 12 17 1
[3,] 3 8 13 18 1
[4,] 4 9 14 19 1
[5,] 5 10 15 20 1
[[2]][[2]]
[,1] [,2] [,3] [,4] [5]
[1,] 1 6 11 16 1
[2,] 2 7 12 17 1
[3,] 3 8 13 18 -1
[4,] 4 9 14 19 1
[5,] 5 10 15 20 -1
Thanks you for any help.
Best wishes, Muhammad Subianto
#Gabor Grothendieck ggrothendieck at gmail.com
#Mon Aug 28 13:53:52 CEST 2006
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 at 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
a slight modification of your routine should work:
#Last week, I asked about merge x and y as list.
#Now I have a dataset with list of list like:
x <- list(list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)),
list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)))
y <- list(list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1)),
list(c(1, 1, 1, 1, 1),c(1, 1, -1, 1, -1)))
x
y
#I need merge x and y, I have tried with
list.uni <- vector("list", length(x))
for (i in 1:length(x)) {
for (j in 1:length(x[[1]])) {
list.uni[[i]][[j]] <- cbind(x[[i]][[j]],y[[i]][[j]])
}
}
list.uni
On 9/3/06, Muhammad Subianto <msubianto at gmail.com>
wrote:> Dear all,
> #Last week, I asked about merge x and y as list.
> #Now I have a dataset with list of list like:
> x <- list(list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)),
> list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)))
> y <- list(list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1)),
> list(c(1, 1, 1, 1, 1),c(1, 1, -1, 1, -1)))
> x
> y
>
> #I need merge x and y, I have tried with
> list.uni <- vector("list", length(x))
> for (i in 1:length(x)) {
> for (j in 1:length(x[[1]])) {
> list.uni[[i]][[j]] <- mapply(cbind,
> x[[i]][[j]],
> y[[i]][[j]],
> SIMPLIFY=FALSE)
> }
> }
> list.uni
>
> I have learn about ?lapply, ?sapply and ?mapply but I still didn't
> understand how to use it.
> I need the result something like
>
>
> [[1]]
> [[1]][[1]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 -1
> [3,] 3 8 13 18 -1
> [4,] 4 9 14 19 1
> [5,] 5 10 15 20 1
>
> [[1]][[2]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 1
> [3,] 3 8 13 18 -1
> [4,] 4 9 14 19 -1
> [5,] 5 10 15 20 -1
>
>
> [[2]]
> [[2]][[1]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 1
> [3,] 3 8 13 18 1
> [4,] 4 9 14 19 1
> [5,] 5 10 15 20 1
>
> [[2]][[2]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 1
> [3,] 3 8 13 18 -1
> [4,] 4 9 14 19 1
> [5,] 5 10 15 20 -1
>
> Thanks you for any help.
> Best wishes, Muhammad Subianto
>
>
>
>
> #Gabor Grothendieck ggrothendieck at gmail.com
> #Mon Aug 28 13:53:52 CEST 2006
>
> 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 at 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 at 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
If z is the result then z[[i]] is formed from x[[i]] and y[[i]] using the previous solution, viz. z <- list() z[[1]] <- mapply(cbind, x[[1]], y[[1]], SIMPLIFY = FALSE) z[[2]] <- mapply(cbind, x[[2]], y[[2]], SIMPLIFY = FALSE) or with a for loop (which is similar to the code you posted below except the extraneous j loop is removed since its already incorporated in the mapply): z <- list() for(i in seq(along = x)) z[[i]] <- mapply(cbind, x[[1]], y[[1]], SIMPLIFY = FALSE) or reducing the loop to a lapply: lapply(seq(along = x), function(i) mapply(cbind, x[[i]], y[[i]], SIMPLIFY = FALSE)) On 9/3/06, Muhammad Subianto <msubianto at gmail.com> wrote:> Dear all, > #Last week, I asked about merge x and y as list. > #Now I have a dataset with list of list like: > x <- list(list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)), > list(matrix(1:20, 5, 4),matrix(1:20, 5, 4))) > y <- list(list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1)), > list(c(1, 1, 1, 1, 1),c(1, 1, -1, 1, -1))) > x > y > > #I need merge x and y, I have tried with > list.uni <- vector("list", length(x)) > for (i in 1:length(x)) { > for (j in 1:length(x[[1]])) { > list.uni[[i]][[j]] <- mapply(cbind, > x[[i]][[j]], > y[[i]][[j]], > SIMPLIFY=FALSE) > } > } > list.uni > > I have learn about ?lapply, ?sapply and ?mapply but I still didn't > understand how to use it. > I need the result something like > > > [[1]] > [[1]][[1]] > [,1] [,2] [,3] [,4] [5] > [1,] 1 6 11 16 1 > [2,] 2 7 12 17 -1 > [3,] 3 8 13 18 -1 > [4,] 4 9 14 19 1 > [5,] 5 10 15 20 1 > > [[1]][[2]] > [,1] [,2] [,3] [,4] [5] > [1,] 1 6 11 16 1 > [2,] 2 7 12 17 1 > [3,] 3 8 13 18 -1 > [4,] 4 9 14 19 -1 > [5,] 5 10 15 20 -1 > > > [[2]] > [[2]][[1]] > [,1] [,2] [,3] [,4] [5] > [1,] 1 6 11 16 1 > [2,] 2 7 12 17 1 > [3,] 3 8 13 18 1 > [4,] 4 9 14 19 1 > [5,] 5 10 15 20 1 > > [[2]][[2]] > [,1] [,2] [,3] [,4] [5] > [1,] 1 6 11 16 1 > [2,] 2 7 12 17 1 > [3,] 3 8 13 18 -1 > [4,] 4 9 14 19 1 > [5,] 5 10 15 20 -1 > > Thanks you for any help. > Best wishes, Muhammad Subianto > > > > > #Gabor Grothendieck ggrothendieck at gmail.com > #Mon Aug 28 13:53:52 CEST 2006 > > 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 at 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 at 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. >
Dear all,
Many thanks to Gabor Grothendieck and Jim Holtman, both of you always
reply (to answer) my problems.
Regards, Muhammad Subianto
##Gabor Grothendieck
If z is the result then z[[i]] is formed from x[[i]] and y[[i]] using
the previous solution, viz.
z <- list()
z[[1]] <- mapply(cbind, x[[1]], y[[1]], SIMPLIFY = FALSE)
z[[2]] <- mapply(cbind, x[[2]], y[[2]], SIMPLIFY = FALSE)
or with a for loop (which is similar to the code you posted below except
the extraneous j loop is removed since its already incorporated
in the mapply):
z <- list()
for(i in seq(along = x))
z[[i]] <- mapply(cbind, x[[1]], y[[1]], SIMPLIFY = FALSE)
or reducing the loop to a lapply:
lapply(seq(along = x), function(i) mapply(cbind, x[[i]], y[[i]],
SIMPLIFY = FALSE))
## Gabor Grothendieck
cbind2 <- function(x, y) {
if (is.list(x))
mapply(cbind2, x, y, SIMPLIFY = FALSE)
else
cbind(x,y)
}
x <- list(list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)),
list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)))
y <- list(list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1)),
list(c(1, 1, 1, 1, 1),c(1, 1, -1, 1, -1)))
cbind2(x,y)
cbind2(x[[1]], y[[1]])
cbind2(x[[1]][[1]], y[[1]][[1]])
## Jim Holtman
list.uni <- vector("list", length(x))
for (i in 1:length(x)) {
for (j in 1:length(x[[1]])) {
list.uni[[i]][[j]] <- cbind(x[[i]][[j]],y[[i]][[j]])
}
}
list.uni
On 9/3/06, Muhammad Subianto <msubianto at gmail.com>
wrote:> Dear all,
> #Last week, I asked about merge x and y as list.
> #Now I have a dataset with list of list like:
> x <- list(list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)),
> list(matrix(1:20, 5, 4),matrix(1:20, 5, 4)))
> y <- list(list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1)),
> list(c(1, 1, 1, 1, 1),c(1, 1, -1, 1, -1)))
> x
> y
>
> #I need merge x and y, I have tried with
> list.uni <- vector("list", length(x))
> for (i in 1:length(x)) {
> for (j in 1:length(x[[1]])) {
> list.uni[[i]][[j]] <- mapply(cbind,
> x[[i]][[j]],
> y[[i]][[j]],
> SIMPLIFY=FALSE)
> }
> }
> list.uni
>
> I have learn about ?lapply, ?sapply and ?mapply but I still didn't
> understand how to use it.
> I need the result something like
>
>
> [[1]]
> [[1]][[1]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 -1
> [3,] 3 8 13 18 -1
> [4,] 4 9 14 19 1
> [5,] 5 10 15 20 1
>
> [[1]][[2]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 1
> [3,] 3 8 13 18 -1
> [4,] 4 9 14 19 -1
> [5,] 5 10 15 20 -1
>
>
> [[2]]
> [[2]][[1]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 1
> [3,] 3 8 13 18 1
> [4,] 4 9 14 19 1
> [5,] 5 10 15 20 1
>
> [[2]][[2]]
> [,1] [,2] [,3] [,4] [5]
> [1,] 1 6 11 16 1
> [2,] 2 7 12 17 1
> [3,] 3 8 13 18 -1
> [4,] 4 9 14 19 1
> [5,] 5 10 15 20 -1
>
> Thanks you for any help.
> Best wishes, Muhammad Subianto
>
>
>
>
> #Gabor Grothendieck ggrothendieck at gmail.com
> #Mon Aug 28 13:53:52 CEST 2006
>
> 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 at 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
>