Finding running maximum by group

不打扰是莪最后的温柔 提交于 2019-11-26 21:04:17

you can do it so:

df$curMax <- ave(df$var, df$group, FUN=cummax)
akrun

We can try data.table. Convert the 'data.frame' to 'data.table' (setDT(df1)), grouped by 'group' , we get the cummax of 'var' and assign (:=) it to a new variable ('curMax')

library(data.table)
setDT(df1)[, curMax := cummax(var), by = group]

As commented by @Michael Chirico, if the data is not ordered by 'time', we can do that in the 'i'

setDT(df1)[order(time), curMax:=cummax(var), by = group]

Or with dplyr

library(dplyr)
df1 %>% 
    group_by(group) %>%
    mutate(curMax = cummax(var)) 

If df1 is tbl_sql explicit ordering might be required, using arrange

df1 %>% 
    group_by(group) %>%
    arrange(time, .by_group=TRUE) %>%
    mutate(curMax = cummax(var)) 

or dbplyr::window_order

library(dbplyr)

df1 %>% 
    group_by(group) %>%
    window_order(time) %>%
    mutate(curMax = cummax(var)) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!