Calculate the average of the condition within the factor [r]

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

##    name target filter
## 1:    A      0      1
## 2:    A      1      2
## 3:    A      2      3
## 4:    B      0      4
## 5:    B      1      5
## 6:    B      2      6
## 7:    C      0      7

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:

##    name   V1 
## 1:    A    2
## 2:    B    2
## 3:  ...
+4
source share
1 answer

You can do it with

dt[, .(meantarget = mean(target[filter == max(filter)])), by = name]
#    name meantarget
# 1:    A      2
# 2:    B      2
# 3:    C      2
# 4:    D      2
# 5:    E      2
# 6:    F      2
# 7:    G      2
+4
source

All Articles