I have below data
CODE | TIME | VALUE |
---|---|---|
ABC | 1 | 40 |
ABC | 2 | 50 |
ABC | 3 | 30 |
ABC | 4 | 60 |
I am trying to find the maximum VALUE
until a given timepoint.
For example, for TIME == 2
and TIME == 3
, MAXVALUE
should be 50.
For TIME == 4
MAXVALUE
should be 60.
I trying to get below table,
CODE | TIME | VALUE | MAXVALUE |
---|---|---|---|
ABC | 1 | 40 | 40 |
ABC | 2 | 50 | 50 |
ABC | 3 | 30 | 50 |
ABC | 4 | 60 | 60 |
Please suggest how can I do in R using dplyr
CodePudding user response:
you can simply use cummax
:
library(dplyr)
df %>%
arrange(time) %>%
mutate(maxvalue = cummax(value))