r - Add rows by groups -


i observe frequencies (eff) of different characters (code) in population @ different dates (date2).

datas <- data.frame(date2 = rep(seq(sys.date() - 2, sys.date(), = "day"), each = 2),                      date1 = sys.date(),                      code = rep(letters[1:2], 3),                      eff = c(50, 30, 20, 10, 20, 20),                      total = 100)  > datas        date2      date1 code eff total 1 2015-07-25 2015-07-27     50   100 2 2015-07-25 2015-07-27    b  30   100 3 2015-07-26 2015-07-27     20   100 4 2015-07-26 2015-07-27    b  10   100 5 2015-07-27 2015-07-27     20   100 6 2015-07-27 2015-07-27    b  20   100 

for each dates2 add code "fill" difference between sum of frequencies day , total population observed day (total).

for example, output i'm expecting :

       date2 eff      date1 total code 1 2015-07-25  50 2015-07-27   100    2 2015-07-25  30 2015-07-27   100    b 3 2015-07-25  20 2015-07-27   100   ko 4 2015-07-26  20 2015-07-27   100    5 2015-07-26  10 2015-07-27   100    b 6 2015-07-26  70 2015-07-27   100   ko 7 2015-07-27  20 2015-07-27   100    8 2015-07-27  20 2015-07-27   100    b 9 2015-07-27  60 2015-07-27   100   ko 

and how produced output :

datas %>%    group_by(date2) %>%    summarise(eff = (sum(total) / n()) - sum(eff)) %>%    inner_join(datas, = "date2") %>%    select(-c(eff.y, code), eff = eff.x) %>%    distinct %>%    mutate(code = "ko") %>%    bind_rows(datas) 

but don't solution , know if has better 1 !

also how 2 grouping variables (date1 & date 2 in example bellow) ?

datas2 <- data.frame(date2 = c(rep(seq(sys.date() - 2, sys.date(), = "day"), each = 2),                                rep(seq(sys.date() - 1, sys.date(), = "day"), each = 2)),                      date1 = c(rep(sys.date() - 3, 6), rep(sys.date() - 2, 4)),                      code = c(rep(letters[1:2], 3), rep(letters[1:2], 2)),                      eff = c(50, 30, 20, 10, 20, 20, 10, 20, 30, 40),                      total = 100) > datas2         date2      date1 code eff total 1  2015-07-25 2015-07-24     50   100 2  2015-07-25 2015-07-24    b  30   100 3  2015-07-26 2015-07-24     20   100 4  2015-07-26 2015-07-24    b  10   100 5  2015-07-27 2015-07-24     20   100 6  2015-07-27 2015-07-24    b  20   100 7  2015-07-26 2015-07-25     10   100 8  2015-07-26 2015-07-25    b  20   100 9  2015-07-27 2015-07-25     30   100 10 2015-07-27 2015-07-25    b  40   100 

thanks ideas !

you may try

 library(dplyr)  datas %>%     group_by(date2, date1) %>%     summarise(eff=total[1l]-sum(eff), code='ko', total=total[1l]) %>%    bind_rows(., datas) %>%    arrange(date2,code)  

or similar data.table approach be

 library(data.table)  rbind(datas,setdt(datas)[, list(eff=total[1l]-sum(eff),            code='ko',total=total[1l]),.(date2,date1)])[order(date2)] 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -