Search code examples
rdplyrnamutated

Mutate - NA Handling


I've used the arrange and mutate combination to do additions based on groupings. For example, I've used the following:

master_df <-group_by(master_df,asof_dt)
mutate(master_df,tot_flag=ls_flag)

This groups my data frame master_df by asof_dt, and then creates tot_flag and adds ls_flag by date.

However, my ls_flag column contains NA's.

I would like to do the following: 1) find out how to add the ls_flag, ignoring any NA's 2) find out how to add the total number of NA's per day.

Here is the full example:

asof_dt<-c("2014-10-01","2014-10-01","2014-10-01","2014-10-02","2014-10-02","2014-10-02")
ls_flag<-c(1,1,NA,NA,1,1)
master_df<-data.frame(asof_dt,ls_flag)
master_df <-group_by(master_df,asof_dt)
mutate(master_df,tot_flag=sum(ls_flag))

Thank you very much!


Solution

  • Is this your desired result? You can use the na.rm = TRUE option in sum():

    master_df %>%
      group_by(asof_dt) %>%
      mutate(tot_flag = sum(ls_flag, na.rm = TRUE),
             tot_NA = sum(is.na(ls_flag)))
    
    #Source: local data frame [6 x 4]
    #Groups: asof_dt
    #
    #     asof_dt ls_flag tot_flag tot_NA
    #1 2014-10-01       1        2      1
    #2 2014-10-01       1        2      1
    #3 2014-10-01      NA        2      1
    #4 2014-10-02      NA        2      1
    #5 2014-10-02       1        2      1
    #6 2014-10-02       1        2      1
    

    Or perhaps you just want a "summary" (using summarise):

    master_df %>%
      group_by(asof_dt) %>%
      summarise(tot_flag = sum(ls_flag, na.rm = TRUE),
                tot_NA = sum(is.na(ls_flag)))
    #Source: local data frame [2 x 3]
    #
    #     asof_dt tot_flag tot_NA
    #1 2014-10-01        2      1
    #2 2014-10-02        2      1