r - Getting column by column name in plyr using summarize inside a function -
i know how use column names sorted in strings using ddply , summarize outside functions, via get, detailed in ddply + summarise function column name input. works when working interactively prompt.
however, doesn't works when try inside function. know there other ways solve problem, detailed in how can use variable names refer data frame columns ddply?. avoid summarise. thing want understand how works (i'm rereading hadley's excellent book, has moved http://adv-r.had.co.nz/)
as similar happens data.table, provide comparison
require(data.table) require(plyr) dt = data.table(alpha = 1:3, beta = head(letters,n=3)) testdt <- function(dt,colname) { dt[,mean(get(colname))] } testplyr <- function(dt,colname) { ddply(.data=dt,.variables=null,.fun=summarise,mean(get(colname))) }
testdt works perfectly
testdt(dt,"alpha")
but testplyr fails when do
testplyr(dt,"alpha")
surely data.table , plyr deal expressions inside calls differently.
i tried with
testplyr <- function(dt,colname) { e = environment() ddply(.data=dt,.variables=null,.fun=summarise,mean(get(colname,envir=e))) }
but doesn't work. ideas??
Comments
Post a Comment