[R] removing particular row from matrix
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Tue Feb 21 19:00:41 CET 2012
> -----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
More information about the R-help
mailing list