3

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!

4

2 回答 2

5

这是你想要的结果吗?您可以使用以下na.rm = TRUE选项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

或者您可能只想要一个“摘要”(使用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
于 2014-11-07T17:15:18.200 回答
3

总和:

> aggregate(ls_flag~asof_dt, data=master_df, sum)
     asof_dt ls_flag
1 2014-10-01       2
2 2014-10-02       2

或者:

> with(master_df, tapply(ls_flag, asof_dt, sum, na.rm=T))
2014-10-01 2014-10-02 
         2          2 

对于 NA 的计数:

> with(master_df, tapply(ls_flag, asof_dt, function(x) sum(is.na(x))))
2014-10-01 2014-10-02 
         1          1 
于 2014-11-07T17:42:58.670 回答