Suppose I have a data.table where each row consists of two vectors:
- A 'pre-subtraction' vector.
- A 'post-subtraction' vector.
The pre-subtraction is the left-half most column and the post- is the right-most columns, with the suffix "prm" at the end.
For example:
#Sample Data
set.seed(2)
fill = data.table(n=1:7)
Tp=3
for(t in 1:Tp){
set(x = fill, j = paste0('v',t), value = sample(0:10,7))
}
fill[1,paste0('v',3):=0]
fill[5,paste0('v',2):=0]
fill[5,paste0('v',3):=0]
for(t in 1:Tp){
fill[,paste0('v',t,'prm'):=get(paste0('v',t))]
}
fill[1,paste0('v',1,'prm'):=0]
fill[2,paste0('v',2,'prm'):=1]
fill[5,paste0('v',3,'prm'):=1]
fill[7,paste0('v',3,'prm'):=2]
The data:
> fill
n v1 v2 v3 v1prm v2prm v3prm
1: 1 2 9 0 0 9 0
2: 2 7 4 8 7 1 8
3: 3 5 10 9 5 10 9
4: 4 1 8 1 1 8 1
5: 5 6 0 0 6 0 1
6: 6 8 7 0 8 7 0
7: 7 0 0 6 0 0 2
A LIFO vector must decrease element-wise to the right before affecting more-left elements. The first row VIOLATES LIFO because
(2, 9, 0) --> (0, 9, 0)
should have subtracted the 2 from the 9 before the 2 on the left-most unit.
I would like to subset to include ONLY the rows that have the 'prm' columns as LIFO subtractions of the non prm columns. E.g.
n v1 v2 v3 v1prm v2prm v3prm
1: 3 5 10 9 5 10 9
2: 4 1 8 1 1 8 1
3: 6 8 7 0 8 7 0
4: 7 0 0 6 0 0 2
EDIT:
LIFO (last-in-first-out) and FIFO (first-in-first-out) are ways of subtraction that prioritize certain elements.
Consider a vector of numbers, (a,b,c). Consider "c" to be the most recent and "a" to be the least recent.
The total number of units in this vector is a+b+c.
If we subtract d units from it, under LIFO or FIFO subtraction, we do not subtract d from every element, rather, we subtract it elementwise from the most recent (LIFO) or least recent (FIFO), until it is depleted (with a min of 0).
for example
LIFO: (3,2,1) - 5 = (3,2,1 - 5) --> (3,2 -4 ,0) --> (3 -2 ,0,0) --> (1,0,0)
FIFO: (3,2,1) - 5 = (3-5,2,1) --> (0,2 -2 ,1) --> (0 ,0,1)