How about eval(substitute
'ing the entire body of the function (or just data.table
calculation if you want to be more specific):
var.report = function(df, value, by.value) {
eval(substitute({
var.report = df[, list( .N,
sum(is.finite(value)), # count values
sum(is.na(value)) # count NA
), by = by.value]
setnames(var.report, c("variable", "N","n.val","n.NA"))
return(var.report)
}))
}
var.report(dt, depth, clarity)
# variable N n.val n.NA
#1: SI2 9194 9194 0
#2: SI1 13065 13065 0
#3: VS1 8171 8171 0
#4: VS2 12258 12258 0
#5: VVS2 5066 5066 0
#6: VVS1 3655 3655 0
#7: I1 741 741 0
#8: IF 1790 1790 0
I don't really understand the second question and I'd normally assign the names in the original expression, which helps keeping track of things better, like so:
var.report = df[, list(N = .N,
n.val = sum(is.finite(value)), # count values
n.NA = sum(is.na(value)) # count NA
)
, by = list(variable = by.value)]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…