tags:

views:

97

answers:

3

In a matrix, if there is some missing data recorded as "NA", how could I delete rows with "NA" in the matrix? Can I use na.rm?

+1  A: 

If you want to remove rows that contain NA's you can use apply() to apply a quick function to check each row. E.g., if your matrix is x,

goodIdx <- apply(x, 1, function(r) !any(is.na(r)))
newX <- x[goodIdx,]
Josh Reich
+4  A: 

I think na.rm usually only works within functions, say for the mean function. I would go with complete.cases: http://stat.ethz.ch/R-manual/R-patched/library/stats/html/complete.cases.htm

let's say you have the following 3x3-matrix:

x <- matrix(c(1:8, NA), 3, 3)

> x
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6   NA

then you can get the complete cases of this matrix with

y <- x[complete.cases(x),]

> y
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8

The complete.cases-function returns a vector of truth values that says whether or not a case is complete:

> complete.cases(x)
[1]  TRUE  TRUE FALSE

and then you index the rows of matrix x and add the "," to say that you want all columns.

jonlemon
+1  A: 

na.omit() will take matrices (and data frames) and return only those rows with no NA values whatsoever - it takes complete.cases() one step further by deleting the FALSE rows for you.

> x <- data.frame(c(1,2,3), c(4, NA, 6))
> x
  c.1..2..3. c.4..NA..6.
1          1           4
2          2          NA
3          3           6
> na.omit(x)
  c.1..2..3. c.4..NA..6.
1          1           4
3          3           6
Matt Parker