我试图在每个星期五对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 ) )
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 )
)
我考虑过使用 dplyr :: summarize 和聚合,但我不知道如何正确设置 by 条件。
mydf %>% group_by( ID ) %>% summarize( Var_Sum = aggregate( Var , sum , by=list ( (mydf$Weekday)=="Friday") ) )
我已经看到使用 cut 函数解决了一些类似的问题,但这似乎是将条件设置为标准周?我还不熟悉它。
答案 0 :(得分:1)
我们需要使用Fragment
cumsum