r - data.table fast acces to column by expression -
i trying column data.table given expression. receive caseid
expression. expr_caseid <- expression(caseid)
. how column expression in fastest possible way?
library(data.table) dt_fcst <- data.table(caseid = as.integer(runif(1e8)*100)) expr_caseid <- expression(caseid) testexpr = function(dt_, expr_){ dt_[[deparse(substitute(expr_))]] } testgetelement = function(dt_, expr_){ getelement(dt_, deparse(substitute(expr_))) } library(microbenchmark) microbenchmark( ## by_char = dt_fcst[['caseid']], by_deparse = testexpr(dt_fcst, caseid), ## by_expr = dt_fcst[, list(caseid)], ## by_dollar = dt_fcst$caseid, by_eval = eval( expr_caseid, envir = as.environment(dt_fcst) ), by_getelement = testgetelement(dt_fcst, caseid) # ,by_index = dt_fcst@.data[[1]] , times = 1000l)
results of performance measurements:
unit: microseconds
expr min lq mean median uq max neval cld by_deparse 37.2 41.35 55.0700 46.15 60.6 357.8 1000 b by_eval 15.9 22.30 29.2194 24.80 34.3 289.8 1000 by_getelement 38.3 42.20 55.9087 47.30 63.2 283.3 1000 b
with comment frank
unit: microseconds expr min lq mean median uq max neval cld by_evalnocoerce 1.8 4.00 8.7652 5.30 7.60 479.8 1000 microbenchmark( ## by_char = dt_fcst[['caseid']], by_deparse = testexpr(dt_fcst, caseid), ## by_expr = dt_fcst[, list(caseid)], ## by_dollar = dt_fcst$caseid, by_eval = eval( expr_caseid, envir = as.environment(dt_fcst) ), by_getelement = testgetelement(dt_fcst, caseid), by_evalnocoerce = eval(expr_caseid, dt_fcst) # ,by_index = dt_fcst@.data[[1]] , times = 1000l)
Comments
Post a Comment