[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,]135 [2,]246 [[2]] [,1] [,2] [,3] [1,]79 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,]135 000 [2,]000 246 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]79 11 000 [2,] 0008 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 000 [2,] 000 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[alternative HTML version deleted]] __ R-help@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.
Re: [R] matrix manipulation with its rows
Hi with your specific example you can use remat-function(mat) { mt-t(mat) mt-c(mt[1:3], rep(0,6), mt[4:6]) matrix(mt, 2,6, byrow=TRUE) } lapply(sample, remat) Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Kathryn Lord Sent: Wednesday, January 16, 2013 9:00 AM To: r-help@r-project.org 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,]135 [2,]246 [[2]] [,1] [,2] [,3] [1,]79 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,]135 000 [2,]000 246 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]79 11 000 [2,] 0008 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 000 [2,] 000 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] matrix manipulation with its rows
Not a great solution, I don't think, but: kronecker(diag(2), matrix(1:6, 2, byrow=TRUE))[c(1,4),] [,1] [,2] [,3] [,4] [,5] [,6] [1,]123000 [2,]000456 So using a function that does this in 'lapply' should solve the problem you state. I'm guessing the real problem might be more complex. Pat On 16/01/2013 07:59, Kathryn Lord wrote: 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,]135 [2,]246 [[2]] [,1] [,2] [,3] [1,]79 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,]135 000 [2,]000 246 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,]79 11 000 [2,] 0008 10 12 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 13 15 17 000 [2,] 000 14 16 18 Any suggestion will be greatly appreciated. Regards, Kathryn Lord [[alternative HTML version deleted]] __ R-help@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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@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.
Re: [R] matrix manipulation with its rows
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.lord2...@gmail.com To: r-help@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@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. __ R-help@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.