Determine the number of NA values in a column

后端 未结 14 1480
眼角桃花
眼角桃花 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:55

    A quick and easy Tidyverse solution to get a NA count for all columns is to use summarise_all() which I think makes a much easier to read solution than using purrr or sapply

    library(tidyverse)
    # Example data
    df <- tibble(col1 = c(1, 2, 3, NA), 
                 col2 = c(NA, NA, "a", "b"))
    
    df %>% summarise_all(~ sum(is.na(.)))
    #> # A tibble: 1 x 2
    #>    col1  col2
    #>    
    #> 1     1     2
    

提交回复
热议问题