Finding running maximum by group
Asked Answered
O

2

5

I need to find a running maximum of a variable by group using R. The variable is sorted by time within group using df[order(df$group, df$time),].

My variable has some NA's but I can deal with it by replacing them with zeros for this computation.

this is how the data frame df looks like:

(df <- structure(list(var = c(5L, 2L, 3L, 4L, 0L, 3L, 6L, 4L, 8L, 4L),
               group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
                                 .Label = c("a", "b"), class = "factor"),
               time = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L)),
          .Names = c("var", "group","time"),
          class = "data.frame", row.names = c(NA, -10L)))

#    var group time
# 1    5     a    1
# 2    2     a    2
# 3    3     a    3
# 4    4     a    4
# 5    0     a    5
# 6    3     b    1
# 7    6     b    2
# 8    4     b    3
# 9    8     b    4
# 10   4     b    5

And I want a variable curMax as:

var  |  group  |  time  |  curMax
5       a         1         5
2       a         2         5
3       a         3         5
4       a         4         5
0       a         5         5
3       b         1         3
6       b         2         6
4       b         3         6
8       b         4         8
4       b         5         8

Please let me know if you have any idea how to implement it in R.

On answered 3/12, 2015 at 15:14 Comment(1)
Found a possible duplicate of this question. Its answers aren't very good, though: https://mcmap.net/q/1873487/-in-r-how-do-i-add-a-max-by-group-duplicate/1191259Piste
P
5

you can do it so:

df$curMax <- ave(df$var, df$group, FUN=cummax)
Patchouli answered 3/12, 2015 at 15:17 Comment(0)
C
8

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)) 
Chard answered 3/12, 2015 at 15:16 Comment(2)
Just a robustness check: if it's not necessarily ordered by time, you could add setDT(df1)[order(time), curMax:=cummax(var), by = group]Esotropia
This is a very old thread. But when I tried it out it worked fine with the data_table option. But the dplyr options didn't start over with cummax for each new group. Am I missing something?Bohemia
P
5

you can do it so:

df$curMax <- ave(df$var, df$group, FUN=cummax)
Patchouli answered 3/12, 2015 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.