abn
abn

Reputation: 1373

Replace a value by its preceding in a column R

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

Answers (1)

G. Grothendieck
G. Grothendieck

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

Related Questions