I have some data set which has some values -999.000 & I would like to remove whole row of this kind of values. e.g a<-matrix(c(1,2,3,4,4,5,6,6,-999.99,5,9,-999.00),nrow=4) a<- [,1] [,2] [,3] [1,] 1 4 -999.99 [2,] 2 5 5.00 [3,] 3 6 9.00 [4,] 4 6 -999.00 expected answer [,1] [,2] [,3] [1,] 2 5 5.00 [2,] 3 6 9.00 I am new in R & I got stuck with this step. Uday -- View this message in context: http://r.789695.n4.nabble.com/removing-particular-row-from-matrix-tp4407401p4407401.html Sent from the R help mailing list archive at Nabble.com.
Hello, Try a<-matrix(c(1,2,3,4,4,5,6,6,-999.99,5,9,-999.00),nrow=4) ix <- apply(a, 1, function(x) sum(trunc(x) != -999) == ncol(a)) a[ix, ] Hope this helps Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/removing-particular-row-from-matrix-tp4407401p4407490.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Feb 21, 2012 at 07:52:20AM -0800, uday wrote:> I have some data set which has some values -999.000 & I would like to remove > whole row of this kind of values. > > e.g > a<-matrix(c(1,2,3,4,4,5,6,6,-999.99,5,9,-999.00),nrow=4) > a<- > [,1] [,2] [,3] > [1,] 1 4 -999.99 > [2,] 2 5 5.00 > [3,] 3 6 9.00 > [4,] 4 6 -999.00 > > expected answer > > [,1] [,2] [,3] > [1,] 2 5 5.00 > [2,] 3 6 9.00 > > I am new in R & I got stuck with this step.Hi. Try this a[rowSums(a == -999 | a == -999.99) == 0, ] [,1] [,2] [,3] [1,] 2 5 5 [2,] 3 6 9 Hope this helps. Petr Savicky.
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of uday > Sent: Tuesday, February 21, 2012 7:52 AM > To: r-help at r-project.org > Subject: [R] removing particular row from matrix > > I have some data set which has some values -999.000 & I would like to > remove > whole row of this kind of values. > > e.g > a<-matrix(c(1,2,3,4,4,5,6,6,-999.99,5,9,-999.00),nrow=4) > a<- > [,1] [,2] [,3] > [1,] 1 4 -999.99 > [2,] 2 5 5.00 > [3,] 3 6 9.00 > [4,] 4 6 -999.00 > > expected answer > > [,1] [,2] [,3] > [1,] 2 5 5.00 > [2,] 3 6 9.00 > > I am new in R & I got stuck with this step. > > Uday >Your example is ambiguous. You specify that you want to remove rows with value of -999.000 from matrix, but then remove a row with value of -999.99. I don't know whether you just have a typographical error or ... You can eliminate rows using logic with indexing. Something like this could work a[!(a[,3] %in% (999.99, -999.00)),] or a[a[,3] != -999.0,] or a[a[,3] > -999,] I.e. in the row index position, place logic that is true only for the rows you want to keep. One caveat is that if you are comparing to floating point numbers, you should read R FAQ 7.31. Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Hi Dan, I am sorry about that it was typo mistake but the both values are -999.99 . your solution works. Thanks Uday -- View this message in context: http://r.789695.n4.nabble.com/removing-particular-row-from-matrix-tp4407401p4409513.html Sent from the R help mailing list archive at Nabble.com.