Measure the max value of all previous values in a data frame
-
12-11-2019 - |
Question
I am trying to make a function that will determine if value in a column of a data frame is a new high. So for example if I had the following data:
x <- rnorm(10,100,sd=5)
x <- data.frame(x)
How can I return, TRUE or FALSE in a new column that only takes into account all the previous values. The resulting table would look something like:
x new.max
1 102.42810 NA
2 109.22762 TRUE
3 101.97970 FALSE
4 101.49303 FALSE
5 93.30595 FALSE
6 96.77199 FALSE
7 110.96441 TRUE
8 96.27485 FALSE
9 101.77163 FALSE
10 100.78992 FALSE
If I try
x$new.max <- ifelse ( x$x == max(x$x) , TRUE, FALSE )
The resulting table is below, as it calculates the maximum value of the entire column instead of a subset of all the previous values.
x new.max
1 102.42810 FALSE
2 109.22762 FALSE
3 101.97970 FALSE
4 101.49303 FALSE
5 93.30595 FALSE
6 96.77199 FALSE
7 110.96441 TRUE
8 96.27485 FALSE
9 101.77163 FALSE
10 100.78992 FALSE
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow