Hi everyone, suppose I have a 2D matrix, is there a command to snip out a specific row/column and then remerge the remaining columns/rows back into a contiguous matrix? I will need to repeat this operation quite a bit(reverse selection). Thanks for any insights you can offer. Yifei
use the '-' feature.>mat <- matrix(rnorm(100), nrow = 10)#snip the second row>mat[-2,]#snip the third column>mat[,-3]#snip rows 5 and 7>mat[-c(5,7),]cheers tc On 10/23/07, yiferic at berkeley.edu <yiferic at berkeley.edu> wrote:> Hi everyone, > > suppose I have a 2D matrix, is there a command to snip out a specific > row/column and then remerge the remaining columns/rows back into a > contiguous matrix? I will need to repeat this operation quite a > bit(reverse selection). > > Thanks for any insights you can offer. > > Yifei > > ______________________________________________ > 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. >-- Tim Calkins 0406 753 997
Matrices are not made of paper! :) If you index a matrix with negative numbers, you'll get back that matrix minus that column or row. A quick example: >a<-matrix(c(1:9),ncol=3) # Create a sample matrix >a # Display it [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 >a[,2] # Get the second column [1] 4 5 6 >a[,-2] # Everything except the second column [,1] [,2] [1,] 1 7 [2,] 2 8 [3,] 3 9 Julian yiferic at berkeley.edu wrote:> Hi everyone, > > suppose I have a 2D matrix, is there a command to snip out a specific > row/column and then remerge the remaining columns/rows back into a > contiguous matrix? I will need to repeat this operation quite a > bit(reverse selection). > > Thanks for any insights you can offer. > > Yifei > > ______________________________________________ > 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.