Programmatic subsetting of a data.table in R -
this feels simple question solution has eluded me 90 minutes of trying, searching , reading manuals , online.
say i've got data.table:
dt<-data.table(a=runif(n = 10),b=runif(n = 10),c=runif(n = 10))
clearly works:
dt[a > 0.5]
and gives me subset of dt values in column "a" greater 0.5. if want bit more flexible (because subset embedded in larger routine).
what i'd make proto-function work:
flexsubset<-function(scolumntosubset,dmin){ subs<-dt[scolumntosubset>dmin] return(subs) }
i've tried without success, among many others...
with=false
any suggestions? many time in advance!
if want pass string, this:
flexsubset = function(scolumntosubset, dmin) dt[get(scolumntosubset) > dmin] flexsubset("a", 0.5)
if want pass unevaluated expression, then:
flexsubset = function(scolumntosubset, dmin) { lhs = substitute(scolumntosubset) dt[eval(lhs) > dmin] } flexsubset(a, 0.5) flexsubset(a / b, 0.5)
Comments
Post a Comment