Skip to content

removing particular row from matrix

9 messages · uday, Rui Barradas, Petr Savicky +4 more

#
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.
#
On Tue, Feb 21, 2012 at 07:52:20AM -0800, uday wrote:
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.
#
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 Petr, 
Thanks for reply 

sorry for late message there was typo error the both values are -999.99 
 a[rowSums(a ==  -999.99) == 0, ], this solution works only if we have to
remove certain value from matrix. 

but if 
a<-matrix(c(1,2,3,5,-999.99,5,-999.99,6,1,5,9,1),nrow=4)  

a
     [,1]    [,2] [,3]
[1,]    1 -999.99    1
[2,]    2    5.00    5
[3,]    3 -999.99    9
[4,]    5    6.00    1

and I would like to remove whole row those have value -999.99 , this
particular function does not work
I got error 

Error in rowSums(a[, 2] == -999.99) : 
  'x' must be an array of at least two dimensions

 it need matrix, so is there any way to deal with this kind of problem. 


Cheers 
Uday 

--
View this message in context: http://r.789695.n4.nabble.com/removing-particular-row-from-matrix-tp4407401p4411348.html
Sent from the R help mailing list archive at Nabble.com.
#
all you need is rowSums(a == -999.99) -- this will check for -999.99
in *any* spot. If you do only want to check a certain column/row, add
drop=FALSE to your subscripting.

Michael
On Wed, Feb 22, 2012 at 1:50 PM, uday <uday_143_4u at hotmail.com> wrote:
#
Is this not what you want:

a[a[,2] != -999.99,]

I didn't see the earlier message so I'm not sure how rowSums
was involved.

Sarah
On Wed, Feb 22, 2012 at 1:50 PM, uday <uday_143_4u at hotmail.com> wrote:

  
    
#
Indeed it must - but you have asked for rowSums on a one-dimensinal object (a[,2]). You didn;t need to sum the rows of that.

Try
a[ a[,2] != -999 , ]

assuming that the column is integer or that you have read the Note on finite representation of ractions in ?Comparison and are willing to take your chances, or 

a[ a[,2] > -998 , ]
if it's not and if it's safe to assume that all large negative numbers are 'missing'.

Better still, if you read the data using read.table, use na.strings=c('"-999", "NA") . That will mark "-999" as missing data. An na.omit will then remove the offending rows (but also those that contain NAs of thre reasons.

S*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}