My question is related to Subset by group with data.table but different.
Imagine a data set like this:
tmp <- data.table(x = 1:10, y = c(27, 70, 54, 18, 50, 44, 22, 73, 6, 5))
For each row of the data, I want to calculate a new value, z, which is the min(y)
for all rows with a larger value of x. For instance, for the third row of the data where x is 3, I want min(y)
among rows with x > 3 (which would be the value 5). For our intents and purposes, you can assume the data is already ordered by x.
At first I thought of using a function like this:
min.y <- function(val, dt) {
dt[x > val, min(y)]
}
But calling tmp[, z:= fun(x, tmp)]
will result in a warning message:
In min(y) : no non-missing arguments to min; returning Inf
What is the proper way to do this?
PS: Obviously, for the last row I expect to get NA as the result