Count no. of values for each column in a dataset

I want to count total no. of values(Non-NA) for each column and want to identify the column with maximum no. of values (Non-NA) .

Kindly suggest how to proceed.

Hi @banoj12,

Try something like this:

library(mice) # loaded for data with missing values (nhanes2)
library(dplyr)

nhanes2 %>% 
  as_tibble() %>% 
  head()
#> # A tibble: 6 x 4
#>   age     bmi hyp     chl
#>   <fct> <dbl> <fct> <dbl>
#> 1 20-39  NA   <NA>     NA
#> 2 40-59  22.7 no      187
#> 3 20-39  NA   no      187
#> 4 60-99  NA   <NA>     NA
#> 5 20-39  20.4 no      113
#> 6 60-99  NA   <NA>    184

count_nonmiss <- 
  nhanes2 %>% 
  summarise_all(~sum(!is.na(.)))

count_nonmiss
#>   age bmi hyp chl
#> 1  25  16  17  15

count_nonmiss %>% 
  pivot_longer(everything()) %>% 
  arrange(desc(value)) %>% 
  slice(1)
#> # A tibble: 1 x 2
#>   name  value
#>   <chr> <int>
#> 1 age      25

Created on 2020-02-07 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.