I want to calculate the simple value of a result variable, but only for the result associated with the maximum instance of another current variable, grouped by factors.
Of course, the calculated statistics can be replaced by any other function, and the estimate within the group can be any other function.
library(data.table) #1.9.5
dt <- data.table(name = rep(LETTERS[1:7], each = 3),
target = rep(c(0,1,2), 7),
filter = 1:21)
dt
#
#
#
#
#
#
#
#
In this frame, the desired result should return the average value for the target that meets the criteria exactly 2.
Sort of:
dt[ , .(mFilter = which.max(filter),
target = target), by = name][ ,
mean(target), by = c("name", "mFilter")]
... seems close, but doesn't hit him rightly.
The solution should return:
#
#
#
#
source
share