Determine the number of NA values in a column

后端 未结 14 1394
眼角桃花
眼角桃花 2020-12-02 04:17

I want to count the number of NA values in a data frame column. Say my data frame is called df, and the name of the column I am considering is

相关标签:
14条回答
  • 2020-12-02 04:31

    You're over-thinking the problem:

    sum(is.na(df$col))
    
    0 讨论(0)
  • 2020-12-02 04:33

    If you are looking for NA counts for each column in a dataframe then:

    na_count <-sapply(x, function(y) sum(length(which(is.na(y)))))
    

    should give you a list with the counts for each column.

    na_count <- data.frame(na_count)
    

    Should output the data nicely in a dataframe like:

    ----------------------
    | row.names | na_count
    ------------------------
    | column_1  | count
    
    0 讨论(0)
  • 2020-12-02 04:33

    This form, slightly changed from Kevin Ogoros's one:

    na_count <-function (x) sapply(x, function(y) sum(is.na(y)))
    

    returns NA counts as named int array

    0 讨论(0)
  • 2020-12-02 04:46
    sapply(name of the data, function(x) sum(is.na(x)))
    
    0 讨论(0)
  • 2020-12-02 04:48

    A tidyverse way to count the number of nulls in every column of a dataframe:

    library(tidyverse)
    library(purrr)
    
    df %>%
        map_df(function(x) sum(is.na(x))) %>%
        gather(feature, num_nulls) %>%
        print(n = 100)
    
    0 讨论(0)
  • 2020-12-02 04:50

    If you are looking to count the number of NAs in the entire dataframe you could also use

    sum(is.na(df))
    
    0 讨论(0)
提交回复
热议问题