Hi, I have a problem regarding matrix handeling. I am working with a serie of matrixes containing several columns. Now I would like to delete those rows of the matrixes,that in one of the columns contain values less than 50 or greater than 1000. How would this be possible, I have tried to create a simple function for this, but I just don't seem to get it right.Thank you so much for your help, Monna _________________________________________________________________ [[elided Hotmail spam]] PLink [[alternative HTML version deleted]]
Try: x <- matrix(sample(10:1100, 100), 10) x[!apply(x < 50 | x > 1000, 1, any),] On Fri, May 2, 2008 at 6:48 AM, Monna Nygård <monnire@hotmail.com> wrote:> > Hi, I have a problem regarding matrix handeling. I am working with a > serie of matrixes containing several columns. Now I would like to delete > those rows of the matrixes,that in one of the columns contain values less > than 50 or greater than 1000. How would this be possible, I have tried to > create a simple function for this, but I just don't seem to get it > right.Thank you so much for your help, Monna > _________________________________________________________________ > [[elided Hotmail spam]] > > PLink > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
> Hi, I have a problem regarding matrix handeling. I am working with > a serie of matrixes containing several columns. Now I would like to > delete those rows of the matrixes,that in one of the columns contain > values less than 50 or greater than 1000.Try this: m <- matrix(runif(150, 0, 1050), nrow=10); m cols.to.remove <- apply(m, 2, function(x) any(x < 50 | x > 1000)) m[,!cols.to.remove] It's well worth learning how to use apply, and its variants (tapply, sapply etc.); they come in very useful. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
you could do it using apply like the first two replies mentioned or you could just use the index way for example: (assuming your matrix name is ym and the values are greater than 25 and less than 50 and the column is column 3).. if you mean "any column" use the first two replies.> ym<-array(runif(10,20,100),c(6,5)); ym[,1] [,2] [,3] [,4] [,5] [1,] 97.57168 52.22471 41.23411 91.78960 22.57583 [2,] 41.17106 23.73572 55.77120 27.47041 37.94204 [3,] 41.23411 91.78960 22.57583 97.57168 52.22471 [4,] 55.77120 27.47041 37.94204 41.17106 23.73572 [5,] 22.57583 97.57168 52.22471 41.23411 91.78960 [6,] 37.94204 41.17106 23.73572 55.77120 27.47041> ym<-ym[(ym[,3]>25) & (ym[,3] <50),]; ym[,1] [,2] [,3] [,4] [,5] [1,] 97.57168 52.22471 41.23411 91.78960 22.57583 [2,] 55.77120 27.47041 37.94204 41.17106 23.73572 thanks Monna Nyg?rd wrote:> > > Hi, I have a problem regarding matrix handeling. I am working with a > serie of matrixes containing several columns. Now I would like to delete > those rows of the matrixes,that in one of the columns contain values less > than 50 or greater than 1000. How would this be possible, I have tried to > create a simple function for this, but I just don't seem to get it > right.Thank you so much for your help, Monna > _________________________________________________________________ > [[elided Hotmail spam]] > > PLink > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/removing-rows-from-matrix-tp17016646p17025836.html Sent from the R help mailing list archive at Nabble.com.