Let's say I have the following data table:
dta <- data.table(
criteria = c('A', 'A', 'B', 'A', 'A', 'B'),
phase = list('block3', c('block1', 'block2'), 'block2', 'block2', 'block3', 'block1'),
start_val = c(12.0, 1.0, 7.0, 7.0, 12.0, 1.0),
end_val = c(15.0, 11.0, 11.0, 11.0, 15.0, 6.0),
max_val = c(13.0, 8.0, 9.5, 11.0, 15.0, 6.0)
)
from which I need the resulting table with two additional column's, cor_start
and cor_end
dtb <- data.table(
criteria = c('A', 'A', 'B', 'A', 'A', 'B'),
phase = list('block3', c('block1', 'block2'), 'block2', 'block2', 'block3', 'block1'),
start_val = c(12.0, 1.0, 7.0, 7.0, 12.0, 1.0),
end_val = c(15.0, 11.0, 11.0, 11.0, 15.0, 6.0),
max_val = c(13.0, 8.0, 9.5, 11.0, 15.0, 6.0),
cor_start = c(12.0, 1.0, 8.0, 9.5, 13.0, 6.0),
cor_end = c(13.0, 8.0, 9.5, 11.0, 15.0, 6.0)
)
the new columns need to be calculated with reference to phase
column by checking if there is any previous row with the current matching phase value.
For better understanding, in this example:
- row 3 has a matching phase of block2 in row 2
- row 4 has a matching phase of block2 in row 3
- row 5 has a matching phase of block3 in row 1
- row 6 has a matching phase of block1 in row 2
however, row 1 and row 2 have no previous matching phase rows. Note that the phase
is of type list.
So, when there is a previous matching row, below are the conditions:
if (max_val in previous matching row is < end_val in current row)
cor_start = previous matching row max_val
cor_end = current row end_val
if (max_val in previous matching row is > end_val in current row)
cor_start = current row end_val
cor_end = current row end_val
and when there is no previous matching row, below are the conditions:
cor_start = current row start_val
cor_end = current row max_val
I looked into shift(), but could not figure out on how to set the above conditions ? Thanks!