Questions about missing data

In the matrix, if there is some missing data recorded as "NA.

  • How can I delete rows with NA in a matrix?
  • Can na.rm be used?
+6
matrix r na
source share
3 answers

na.omit() will take matrices (and data frames) and return only those rows without any NA values ​​- another complete.cases() step is required, 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 
+6
source share

I think na.rm usually only works in functions, say, for the middle 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 full 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 the case is complete:

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

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

+5
source share

If you want to remove lines containing NA, you can use apply () to apply a quick function to check each line. For example, if your matrix is ​​x,

 goodIdx <- apply(x, 1, function(r) !any(is.na(r))) newX <- x[goodIdx,] 
+1
source share

All Articles