How to find position of missing values in a vector

笑着哭i 提交于 2019-12-13 22:47:48

问题


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.



来源:https://stackoverflow.com/questions/29502994/how-to-find-position-of-missing-values-in-a-vector

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!