Home > Software design >  Sum over a day plus following N number of days in R
Sum over a day plus following N number of days in R

Time:12-13

I have following dataframe:

df <- structure(list(DateTime = structure(c(1477938600, 1477940400, 1477942200, 1477944000, 1477945800, 1477947600, 1477949400, 1477951200, 1477953000, 1477954800, 1477956600, 1477958400, 1477960200, 1477962000, 1477963800, 1477965600, 1477967400, 1477969200, 1477971000, 1477972800, 1477974600, 1477976400, 1477978200, 1477980000, 1477981800, 1477983600, 1477985400, 1477987200, 1477989000, 1477990800, 1477992600, 1477994400, 1477996200, 1477998000, 1477999800, 1478001600, 1478003400, 1478005200, 1478007000, 1478008800, 1478010600, 1478012400, 1478014200, 1478016000, 1478017800, 1478019600, 1478021400, 1478023200, 1478025000, 1478026800, 1478028600, 1478030400, 1478032200, 1478034000, 1478035800, 1478037600, 1478039400, 1478041200, 1478043000, 1478044800, 1478046600, 1478048400, 1478050200, 1478052000, 1478053800, 1478055600, 1478057400, 1478059200, 1478061000, 1478062800, 1478064600, 1478066400, 1478068200, 1478070000, 1478071800, 1478073600, 1478075400, 1478077200, 1478079000, 1478080800, 1478082600, 1478084400, 1478086200, 1478088000, 1478089800, 1478091600, 1478093400, 1478095200, 1478097000, 1478098800, 1478100600, 1478102400, 1478104200, 1478106000, 1478107800, 1478109600, 1478111400, 1478113200, 1478115000, 1478116800, 1478118600, 1478120400, 1478122200, 1478124000, 1478125800, 1478127600, 1478129400, 1478131200, 1478133000, 1478134800, 1478136600, 1478138400, 1478140200, 1478142000, 1478143800, 1478145600, 1478147400, 1478149200, 1478151000, 1478152800, 1478154600, 1478156400, 1478158200, 1478160000, 1478161800, 1478163600, 1478165400, 1478167200, 1478169000, 1478170800, 1478172600, 1478174400, 1478176200, 1478178000, 1478179800, 1478181600, 1478183400, 1478185200, 1478187000, 1478188800, 1478190600, 1478192400, 1478194200, 1478196000, 1478197800, 1478199600, 1478201400, 1478203200, 1478205000, 1478206800), class = c("POSIXct", "POSIXt"), tzone = "Etc/GMT-6"), val = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3, 4, 6, 6, 7, 5, 4, 6, 7, 6, 4, 5, 6, 5, 2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2, 3, 5, 5, 6, 7, 8, 12, 12, 9, 11, 13, 14, 12, 11, 9, 7, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 150L), class = "data.frame")

I need to calculate sum of non-NA values over a day plus following 2 days. I tried following code but It outputs sum only over a day but I need sum over a day plus following two days.

df <- df %>% 
  group_by(date(DateTime)) %>% 
  mutate(vals_count = sum(!(is.na(val))))

CodePudding user response:

Perhaps, if the OP meant a rolling sum, do the group by sum of logical and then use rollsum or rollapply

library(zoo)
library(dplyr)
df %>%
    group_by(date = as.Date(DateTime)) %>% 
    summarise(count = sum(!is.na(val))) %>% 
    mutate(count3 = rollapply(count, width = 3, 
        partial = TRUE, FUN = sum, align = 'left'))
  • Related