# [R] matrix manipulation with its rows

arun smartpink111 at yahoo.com
Wed Jan 16 14:22:16 CET 2013

```HI,

You could also do this:

lapply(sample1,function(x) {mat1<-cbind(matrix(0,nrow=2,ncol=3),x); mat1[cbind(rep(1,3),1:3)]<- mat1[cbind(rep(1,3),4:6)]; mat1[cbind(rep(1,3),4:6)]<-0; mat1})
A.K.
----- Original Message -----

From: Kathryn Lord <kathryn.lord2000 at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Wednesday, January 16, 2013 2:59 AM
Subject: [R] matrix manipulation with its rows

Dear R users,

I have a question about matrix manipulation with its rows.

Plz see the simple example below

sample <- list(matrix(1:6, nr=2,nc=3), matrix(7:12, nr=2,nc=3),
matrix(13:18,nr=2,nc=3))

> sample
[[1]]
[,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

[[2]]
[,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

[[3]]
[,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

With this list, I'd like to create this below

[[1]]
[,1] [,2] [,3]  [,4] [,5] [,6]
[1,]    1    3    5     0    0    0
[2,]    0    0    0   2    4    6

[[2]]
[,1] [,2] [,3]   [,4] [,5] [,6]
[1,]    7    9   11     0    0    0
[2,]     0    0    0    8   10   12

[[3]]
[,1] [,2] [,3]       [,4]   [,5]   [,6]
[1,]   13   15   17      0    0    0
[2,]       0    0    0   14   16   18

Any suggestion will be greatly appreciated.

Regards,

Kathryn Lord

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help