How to find position of missing values in a vector

后端 未结 2 1599
死守一世寂寞
死守一世寂寞 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
    

提交回复
热议问题