I have a column which has values like below:
x <- c(23, 17, 25, 22, 21, 51, 22, NA, 62, 27, NA, NA, 27, 50, NA,
NA, 22, 19, 27, 27, NA, 25, NA, NA, 25)
I want to create a new column which indicates whether the value in the above column till that row is the maximum value so far.
So, for the first row, the new column entry will be 1 since 23 is the highest first value.
The second row in the new column will be 0 since 17 is not the maximum of 23 17
The third row in the new column will be 1 since 25 is the maximum of 23 17 25
And so on...
The new column should be like
1 0 1 0 0 1 0 ..
Not sure how to do this, can think of loops but there must be a crisper way.
NA's should be kept in place
Assuming you want to preserve NAs, you can do
(x==cummax(ifelse(!is.na(x), x, min(x, na.rm=T)))) + 0
# [1] 1 0 1 0 0 1 0 NA 1 0 NA NA 0 0 NA NA 0 0 0 0 NA 0 NA NA 0
Basically turn all the NA values into the lowest observed value and then see if x
is equal to the cumulative max
If there were no NA values this would just be
(x==cummax(x)) + 0
(The +0
just turns TRUE/FALSE values into 1/0)