How to add value to previous row if condition is met

grig109

What I am trying to do is if a row in column b is equal to 2 then I want to add 3 to the value of the previous row in column a. And if the condition is not met, then I don't want the previous row value to change. When I try this code however R is adding to the subsequent row. And it doesn't matter what I replace the -1 row reference with it always adds to the subsequent row.

df$a[-1] <- ifelse(df$b == 2, df$a[-1] + 3, df$a[-1])

Have                               Want
a b                                a b
0 1                                0 1
1 3                                4 3
2 2                                2 2
2 4                                2 4
4 5                                4 5
dimitris_ps

Another approach

df <- data.frame(a=c(0, 1, 2, 2, 3),
             b=c(1, 3, 2, 4, 4))

df$a[c(df$b[-1], 0) == 2] <- df$a[c(df$b[-1], 0) == 2] + 3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the value in a row if condition met in the previous row under dplyr

Pandas: Replace value of column with previous row value if condition is met

How to replace a pandas column row with the previous row if a condition is met

Is there a way to store previous value in a row and change when a new condition is met?

Dataframe how to fill column with previous value until a condition is met

How to add a new column to a dataframe in condition on the previous row of the desired value to be all NAs?

How to get value from upcomming row if condition is met?

One of the previous row value with condition

Take latest row if condition met else previous row

How can I get a previous row from where the condition is met in data frame in Pandas

how to add postscript if condition is met

Add quantity to pandas column if row condition is met

Excel VBA To Add New Row If Condition Is Met

R: Find a value in rows met a condition, add certain numbers to the value, and then create a message in that row in another column

SQL: How to fill empty cells with previous row value on basis of condition?

Pandas duplicating row if condition met, and assigning value

Update row value with column name if condition is met

Add row value to previous row value in R

Add additional key and value on arrays if condition is met

How to jump a row in a while loop if condition met

How to add a value from a row to the previous one in a column in R?

Evaluate a condition in a row and return a certain value if the condition is met

Extract value from previous row based on a condition

Replace value with previous row if match the condition

R fill in NA with previous row value with condition

Pandas compare value with previous row with filtration condition

How to find string only if one condition is met and drop value from cell in dataframe but not the row itself?

How to fetch a column header if a particular condition is met based on row and column value of the dataframe?

Repeat row of dataframe if condition met, and change value of one value