How to check whether a vector is LIFO/FIFO decreasing
Here is a possible approach to calculate the lifo vector first before filter for those rows with lifo vectors:
#convert into long format from MichaelChirico and svenkatesh
tbl <- melt(fill, meas=patterns("^v[1-9]$", "prm$"),
value.name=c("bef","aft"))
setorder(tbl, n, -variable)
#filter for those lifo vector
fill[n %in%
tbl[, {
#calculate stock taken out
dif <- sum(bef) - sum(aft)
#calculate lifo vector
lifo <- pmin(pmax(cumsum(bef) - dif, 0L), bef)
#check if after is this lifo vector
identical(lifo, aft)
}, by=.(n)][(V1), n]
]
output:
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
data:
library(data.table)
fill <- structure(list(n = 1:7, v1 = c(2L, 7L, 5L, 1L, 6L, 8L, 0L), v2 = c(9L,
4L, 10L, 8L, 0L, 7L, 0L), v3 = c(0L, 8L, 9L, 1L, 0L, 0L, 6L),
v1prm = c(0L, 7L, 5L, 1L, 6L, 8L, 0L), v2prm = c(9L, 1L,
10L, 8L, 0L, 7L, 0L), v3prm = c(0L, 8L, 9L, 1L, 1L, 0L, 2L
)), row.names = c(NA, -7L), class = c("data.table", "data.frame"
))
To reiterate the approach from @chinsoon12 and @MichaelChirico in the comments:
Here is fill
:
n prod1vint1 prod1vint2 prod1vint3 prod1vint1prm prod1vint2prm prod1vint3prm
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
# Melt so that the data from the "prm" columns are different from the "prod" columns
d = melt(fill, measure.vars = patterns("int[1-9]$", "prm$"))
# Subtract the vectors and check whether the difference is increasing (LIFO condition)
s = d[, !is.unsorted(value1 - value2), by=.(n)]
# Select the rows that satisfy the LIFO condition
output = fill[n %in% d[, s[(V1), n]], ]
Here is the output:
n prod1vint1 prod1vint2 prod1vint3 prod1vint1prm prod1vint2prm prod1vint3prm
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