How to find position of missing values in a vector

后端 未结 2 1600
死守一世寂寞
死守一世寂寞 2021-01-29 12:40

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?

相关标签:
2条回答
  • 2021-01-29 13:24
    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
    
    0 讨论(0)
  • 2021-01-29 13:26

    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.

    0 讨论(0)
提交回复
热议问题