r - pass variables and names to data.table function -
i have study needs applied different names of data.tables [both j , by]. way done wrapping arguments in eval(substitute(value)) function. makes code less readable. have named j argument "variable", pass j argument of function setnames functions.
so, questions are:
is there way avoid eval(substitute(value)) construction?
can pass j argument setnames function?
library(data.table) library(ggplot2) data(diamonds, bundle = "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 ), = eval(substitute(by.value))] setnames(var.report, c("variable", "n","n.val","n.na")) return(var.report) } var.report(dt, depth, clarity)
how eval(substitute'ing entire body of function (or data.table calculation if want 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.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 understand sec question , i'd assign names in original expression, helps keeping track of things better, so:
var.report = df[, list(n = .n, n.val = sum(is.finite(value)), # count values n.na = sum(is.na(value)) # count na ) , = list(variable = by.value)] r data.table
No comments:
Post a Comment