Search code examples
rdplyrdata-cleaning

Count number of NA's in a Row in Specified Columns R


I want to be able to count the number of NA's that appear in a row in specified columns. From my data below, I'd like to be able to count the NA's rowwise that appear in first, last, address, phone, and state columns (exlcuding m_initial and customer in the count).

    first   m_initial     last         address            phone      state  customer 
    Bob         L         Turner       123 Turner Lane    410-3141   Iowa   NA        
    Will        P         Williams     456 Williams Rd    491-2359   NA     Y        
    Amanda      C         Jones        789 Haggerty       NA         NA     Y        
    Lisa        NA        Evans        NA                 NA         NA     N        

Desired output:

    first   m_initial   last       address            phone      state  customer na_count 
    Bob     L           Turner     123 Turner Lane    410-3141   Iowa   NA       0 
    Will    P           Williams   456 Williams Rd    491-2359   NA     Y        1
    Amanda  C           Jones      789 Haggerty       NA         NA     Y        2
    Lisa    NA          Evans      NA                 NA         NA     N        3  

Solution

  • df$na_count <- rowSums(is.na(df[c('first', 'last', 'address', 'phone', 'state')])) 
    
    df
       first m_initial     last         address    phone state customer na_count
    1    Bob         L   Turner 123 Turner Lane 410-3141  Iowa     <NA>        0
    2   Will         P Williams 456 Williams Rd 491-2359  <NA>        Y        1
    3 Amanda         C    Jones    789 Haggerty     <NA>  <NA>        Y        2
    4   Lisa      <NA>    Evans            <NA>     <NA>  <NA>        N        3