I am trying to sum a variable on a data.frame for every Friday.
Random data frame
mydf = data.frame( "ID" = c( rep( "A" , 6) , rep( "B" , 5 ) ), "Date" = c( "2017-09-08","2017-09-10","2017-09-13","2017-09-15","2017-09-20","2017-09-22","2017-08-03","2017-08-04","2017-08-10","2017-08-11","2017-08-12" , "Var" = c( 1,2,3,4,5,6,7,8,NA,10,11) )
mydf$Date = as.Date( mydf$Date )
mydf = cbind( mydf , "WeekDay" = weekdays( mydf$Date ) )
What I want to get
df_ToGet =
data.frame(
"ID" = c( rep( "A" , 3) , rep( "B" , 2 ) ),
"Date" = c( "2017-09-08","2017-09-15","2017-09-22","2017-08-04","2017-08-11" ),
"Var_Sum" = c( 1 , 9 , 11 , 15, 10 )
)
What I tried
I have considered using dplyr::summarize and aggregate but I do not know how to set the by condition properly.
mydf %>% group_by( ID ) %>% summarize( Var_Sum = aggregate( Var , sum , by=list ( (mydf$Weekday)=="Friday") ) )
I have seen a few similar questions being solved using the cut function but that seems to be setting the condition to a standard week? I'm not too familiar with it yet.