r - Assign value to row if row and previous row meet requirement -
these events , x , y coordinates of each event. looking assign value of "1" event if event = 0 < x < 4 & 0 < y < 4 , previous event = x > 4 & y > 4, , assign value of "0" if criteria not met. here initial table:
event locx locy 1 6 6 2 3 2 3 3 7 4 1 4 5 7 4 6 1 2 7 8 5 8 1 1
my final table like:
event locx locy value 1 6 6 0 2 3 2 1 3 3 7 0 4 1 4 0 5 7 4 0 6 1 2 1 7 8 5 0 8 1 1 1
any appreciated!
another data.table
approach. translation of neilfws's dplyr approach while using shift
function compare values previous one.
library(data.table) setdt(dt)[ ,value := ifelse(locx < 4 & locy < 4 & shift(locx, type = "lag") > 4 & shift(locy, type = "lag") > 4, 1, 0)] dt event locx locy value 1: 1 6 6 0 2: 2 3 2 1 3: 3 3 7 0 4: 4 1 4 0 5: 5 7 4 0 6: 6 1 2 0 7: 7 8 5 0 8: 8 1 1 1
data
dt <- read.table(text = "event locx locy 1 6 6 2 3 2 3 3 7 4 1 4 5 7 4 6 1 2 7 8 5 8 1 1", header = true)
Comments
Post a Comment