How to find position of missing values in a vector

2020-05-10 23:59发布

问题:

What features does the R language have to find missing values in dataframe or at least, how to know that the dataframe has missing values?

回答1:

x = matrix(rep(c(NA, 1,NA), 3), ncol=3, nrow=3)
print(x)
     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]    1    1    1
[3,]   NA   NA   NA

matrix of boolean values: is the value NA

is.na(x)
      [,1]  [,2]  [,3]
[1,]  TRUE  TRUE  TRUE
[2,] FALSE FALSE FALSE
[3,]  TRUE  TRUE  TRUE

indices of NA values:

which(is.na(x), arr.ind = T)
     row col
[1,]   1   1
[2,]   3   1
[3,]   1   2
[4,]   3   2
[5,]   1   3
[6,]   3   3

see if the matrix has any missing values:

any(is.na(x))
TRUE


回答2:

It's hard to tell based on the example you've given, more details on the structure of "data" would be helpful, but, if you simply want to exclude any observation (row) of your data that has a missing value anywhere in it, try:

cleanDat <- na.omit(data)

Note, there is a nice tutorial on missing data which is where I looked to confirm I had this right.