Reputation: 1373
I have a dataframe, where I'm trying to replace values in a column, if they're lesser than their preceding ones as shown in the example below, with their preceding ones. I know how to replace NA's but I'm confused with this condition.
Present column:
0.1
0.1
0.1
0.01
1
Required column:
0.1
0.1
0.1
0.1
1
Upvotes: 1
Views: 99
Reputation: 269431
Try this. It will ensure that column x
is non-decreasing:
DF <- data.frame(x = c(0.1, 0.1, 0.1, 0.01, 1)) # test data
transform(DF, x = cummax(x))
Upvotes: 2