I have a report that needs to be applied for different names of data.tables [both j and by].
The only way I get it done it by wrapping the arguments in an eval(substitute(value))
function. This makes the code less readable.
I have named the j argument "variable", but I would like to pass the j argument of the function to the setnames
functions.
So, the questions are:
is there a way to avoid the eval(substitute(value))
construction?
can I pass the j argument to the setnames function?
library(data.table)
library(ggplot2)
data(diamonds, package = "ggplot2")
dt = as.data.table(diamonds)
var.report = function(df, value, by.value) {
var.report = df[, list( .N,
sum(is.finite(eval(substitute(value)))), # count values
sum(is.na(eval(substitute(value)))) # count NA
), by = eval(substitute(by.value))]
setnames(var.report, c("variable", "N","n.val","n.NA"))
return(var.report)
}
var.report(dt, depth, clarity)
See Question&Answers more detail:os