Column value incremental update

Emma

Fellow R-Stackoverflowers,

I have a data table with 2 columns and I'm trying to calculate a new third column based on both the 2 existing column values and the new column calculated value for the previous row.

I have been checking the forums and I have tried a couple of answers but I don't get it right. I hope you can help me.

Here is a reproducible example:

error <- c(1,1,0,0,0,1,1,1,1,0)
trigger <- c(FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE)
expected <- c(1,2,0,0,0,1,2,3,4,0)
DTtest <- data.table(error, trigger, expected)
DTtest
     error trigger expected 
 1:     1   FALSE        1     
 2:     1    TRUE        2     
 3:     0   FALSE        0     
 4:     0   FALSE        0     
 5:     0   FALSE        0     
 6:     1   FALSE        1     
 7:     1    TRUE        2     
 8:     1    TRUE        3     
 9:     1    TRUE        4     
10:     0   FALSE        0     

The "expected" column includes the values I expect to calculate with the "error" and "trigger" columns. The formula I would like to apply would be the following:

if(trigger) {
    new_column = new_column(previous_row) + 1
} else {
    new_column = error
}

My first try was to use an ifelse directly to update the new column. I found that I had to actually initialize the new column for it to run:

DTtest <- DTtest[, impact:=0]
DTtest[, impact:=ifelse(trigger, lag(impact)+1, error)]

This option does calculate the new column "impact" but the results do not match the expected value I got calculating the column in Excel (something I can't do with the whole data table, as it's pretty big):

    error trigger expected impact
 1:     1   FALSE        1      1
 2:     1    TRUE        2      1
 3:     0   FALSE        0      0
 4:     0   FALSE        0      0
 5:     0   FALSE        0      0
 6:     1   FALSE        1      1
 7:     1    TRUE        2      1
 8:     1    TRUE        3      1
 9:     1    TRUE        4      1
10:     0   FALSE        0      0

Then I tried a for loop but the results are not correct either:

for(index in nrow(DTtest)){
  imp <- 0
  if(index==1){
    imp <- DTtest[index]$error
  } else {
    imp <- DTtest[index-1]$impact+1
  }
  set(DTtest, i=index, j=as.integer(4), value=imp )
}

I have the feeling that lag(impact) does not get the updated value for some reason, but I can't fancy why.

Would you please help me? .

Thank you!

Colonel Beauvel

Here is an alternative approach using groups:

DTtest[, grp:=cumsum(!trigger)][,new:=c(error[1], cumsum(head(error, -1))+1),grp][]

    error trigger expected grp new
 1:     1   FALSE        1   1   1
 2:     1    TRUE        2   1   2
 3:     0   FALSE        0   2   0
 4:     0   FALSE        0   3   0
 5:     0   FALSE        0   4   0
 6:     1   FALSE        1   5   1
 7:     1    TRUE        2   5   2
 8:     1    TRUE        3   5   3
 9:     1    TRUE        4   5   4
10:     0   FALSE        0   6   0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update a column with a calculated value

From Dev

update DataGridView column value

From Dev

Update column to a value in joined column

From Dev

Assign incremental seed to field based on another column value

From Dev

MySQL SELECT incremental index based on value in a different column

From Dev

Update value of other column on update of this column

From Dev

Update Values with incremental count

From Dev

Want incremental update in SQL

From Dev

update value in column and have a trigger update another column depending on the value

From Dev

How to update column value in laravel

From Dev

Update column, replace part of value

From Dev

delete or update depending column value

From Dev

oracle: update a column with not null value

From Dev

How to update column value in laravel

From Dev

Update a value in a column in Parse class

From Dev

update a certain column with a set value

From Dev

Update column value with diiferent condition

From Dev

update value with sum of multiple column

From Dev

Incremental insert into SQL column

From Dev

How to update a field's value by an incremental value without using loop in SQL Server 2008?

From Dev

MySQL update column which is a value in another column

From Dev

UPDATE column if column is greater than a value

From Dev

MySQL update column which is a value in another column

From Dev

Update column from another table column value

From Dev

Update a column based on the value of other column

From Dev

How to update a column using another column value?

From Dev

update a column of numbers from value to (value * 1.3)

From Dev

How to insert rows and getting an incremental value if I have NOT setup IDENTITY for a column?

From Dev

Update column with certain value to a column value with same id

Related Related

  1. 1

    Update a column with a calculated value

  2. 2

    update DataGridView column value

  3. 3

    Update column to a value in joined column

  4. 4

    Assign incremental seed to field based on another column value

  5. 5

    MySQL SELECT incremental index based on value in a different column

  6. 6

    Update value of other column on update of this column

  7. 7

    Update Values with incremental count

  8. 8

    Want incremental update in SQL

  9. 9

    update value in column and have a trigger update another column depending on the value

  10. 10

    How to update column value in laravel

  11. 11

    Update column, replace part of value

  12. 12

    delete or update depending column value

  13. 13

    oracle: update a column with not null value

  14. 14

    How to update column value in laravel

  15. 15

    Update a value in a column in Parse class

  16. 16

    update a certain column with a set value

  17. 17

    Update column value with diiferent condition

  18. 18

    update value with sum of multiple column

  19. 19

    Incremental insert into SQL column

  20. 20

    How to update a field's value by an incremental value without using loop in SQL Server 2008?

  21. 21

    MySQL update column which is a value in another column

  22. 22

    UPDATE column if column is greater than a value

  23. 23

    MySQL update column which is a value in another column

  24. 24

    Update column from another table column value

  25. 25

    Update a column based on the value of other column

  26. 26

    How to update a column using another column value?

  27. 27

    update a column of numbers from value to (value * 1.3)

  28. 28

    How to insert rows and getting an incremental value if I have NOT setup IDENTITY for a column?

  29. 29

    Update column with certain value to a column value with same id

HotTag

Archive