0

I'm trying to make a running maximum column by group in a dataframe. I can do this with a loop through the following but I was wondering if there was a dplyr way of doing this:

df <- data.frame(class=rep(LETTERS,each=20),
                          value=100*rpois(20*26,10))

df$runningMax <- df$value

for (i in 2:nrow(df)) {
  if (df$class[i]==df$class[i-1]) {
    # Running max of class
    df$runningMax[i] = max(df$runningMax[i],df$runningMax[i-1])
  }
}

This was my dplyr attempt:

x <- df %>%
  group_by(class) %>%
      mutate(running_max = value) %>%
      mutate(running_max = max(running_max, lag(running_max)))

Thanks for the help!

zx8754
  • 52,746
  • 12
  • 114
  • 209
tonyk
  • 348
  • 5
  • 22
  • 4
    Take a look at the function `cummax` – Joris Meys Feb 27 '17 at 11:51
  • Outside a pipeline it works but inside it gives and error, any ideas? `x <- df %>% ave(df$value, df$class, FUN=cummax)` gives an error `Error in Math.data.frame(X[[i]], ...) : non-numeric variable in data frame: class ` – tonyk Feb 27 '17 at 12:03
  • 1
    `df %>% {ave(df$value, df$class, FUN=cummax)}` or `df %>% group_by(class) %>% mutate(x = cummax(value))` – Axeman Feb 27 '17 at 12:12
  • Perfect, thank you! – tonyk Feb 27 '17 at 12:36

0 Answers0