Flag a row in one column based on past value in another column

Bucket

Probably basic, but I'm banging my head against a wall.

Say I have this data table:

library(data.table)
df <- data.frame(DaystoPeak = c(2,NA,NA,NA,NA,NA,3,NA,NA,NA,NA,NA), 
                 FlagPeak = c(0,0,1,0,0,0,0,0,0,1,0,0))

df <- data.table(df)

df
 #   DaystoPeak FlagPeak
 #1:          2        0
 #2:         NA        0
 #3:         NA        1
 #4:         NA        0
 #5:         NA        0
 #6:         NA        0
 #7:          3        0
 #8:         NA        0
 #9:         NA        0
 #10:        NA        1
 #11:        NA        0
 #12:        NA        0

Basically what I want to do is generate the second column. I was trying out something like:

df <- df[, FlagPeak := 0] 
df <- df[, FlagPeak[.I+2] := ifelse(DaystoPeak == 2, 1, FlagPeak)] 

But it doesn't work... probably a circular reference problem or something. I've tried a few other things but they don't work either :(

Eg:

df <- df[, FlagPeak := ifelse(DaystoPeak == 2, c(0,0,1), FlagPeak)] 

Anyone able to lend a hand?

eddi

I think the comments have a lot of good options. I'd probably do this though:

df[, FlagPeak := 0][DaystoPeak + 1:.N, FlagPeak := 1]

This works because df[NA, FlagPeak := 1] doesn't do anything.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Updating one column based on the value of another column

From Dev

How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe?

From Dev

Pandas assign value of one column based on another

From Java

Pandas/Python: Set value of one column based on value in another column

From Dev

Copy value from one column based on the value of another column

From Dev

Pandas/Python: Set value of one column based on value in another column

From Dev

Copy value from one column based on the value of another column

From Dev

Ignore a row in SQL if one column value is equal and another column is not equal

From Dev

SELECT Mysql - Replacing value in one column based on another column

From Dev

Replace NaN's in one column with string, based on value in another column

From Dev

groupby and withhold information of one column based on value of another column

From Dev

Replace NaN's in one column with string, based on value in another column

From Dev

Setting the value of one column based on three conditions of another column

From Java

Create a column that assigns value to a row in a dataframe based on an event in another row

From Dev

Select row based upon the MAX of one column and the related MIN of another

From Dev

obtain corresponding value of one column based on minimum value of another in R

From Dev

calculate sum based on value of other row in another column

From Dev

VBA - Pulling a value on the same row based on another column

From Dev

Count cells based on a comparison with value in the same row of another column

From Dev

Selecting a column name dynamically (based on another table row's value)

From Dev

updating column value based on another column value

From Dev

Check if one value in one column is in another column

From Dev

Check if one value in one column is in another column

From Dev

Return one row in a view based on a left join column value

From Dev

subtracting values in one column based on another column

From Dev

Remove duplicates in one column based on another column

From Dev

Update column in one table based on value in another table in mysql

From Dev

Update column value from one position to another position based on indexed

From Dev

Update column in one table based on value in another table in mysql

Related Related

  1. 1

    Updating one column based on the value of another column

  2. 2

    How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe?

  3. 3

    Pandas assign value of one column based on another

  4. 4

    Pandas/Python: Set value of one column based on value in another column

  5. 5

    Copy value from one column based on the value of another column

  6. 6

    Pandas/Python: Set value of one column based on value in another column

  7. 7

    Copy value from one column based on the value of another column

  8. 8

    Ignore a row in SQL if one column value is equal and another column is not equal

  9. 9

    SELECT Mysql - Replacing value in one column based on another column

  10. 10

    Replace NaN's in one column with string, based on value in another column

  11. 11

    groupby and withhold information of one column based on value of another column

  12. 12

    Replace NaN's in one column with string, based on value in another column

  13. 13

    Setting the value of one column based on three conditions of another column

  14. 14

    Create a column that assigns value to a row in a dataframe based on an event in another row

  15. 15

    Select row based upon the MAX of one column and the related MIN of another

  16. 16

    obtain corresponding value of one column based on minimum value of another in R

  17. 17

    calculate sum based on value of other row in another column

  18. 18

    VBA - Pulling a value on the same row based on another column

  19. 19

    Count cells based on a comparison with value in the same row of another column

  20. 20

    Selecting a column name dynamically (based on another table row's value)

  21. 21

    updating column value based on another column value

  22. 22

    Check if one value in one column is in another column

  23. 23

    Check if one value in one column is in another column

  24. 24

    Return one row in a view based on a left join column value

  25. 25

    subtracting values in one column based on another column

  26. 26

    Remove duplicates in one column based on another column

  27. 27

    Update column in one table based on value in another table in mysql

  28. 28

    Update column value from one position to another position based on indexed

  29. 29

    Update column in one table based on value in another table in mysql

HotTag

Archive