简体   繁体   中英

Programmatic subsetting of a data.table in R

This feels like a really simple question but its solution has eluded me for about 90 minutes of trying, searching and reading manuals and online.

Say I've got a data.table:

DT<-data.table(a=runif(n = 10),b=runif(n = 10),c=runif(n = 10))

Clearly something like this works:

DT[a > 0.5]

and gives me the subset of DT where the values in column "a" are greater than 0.5. But what if I want to be a bit more flexible (because the subset is embedded in a larger routine).

What I'd like to do is make this 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 thanks for your time in advance!

If you want to pass a string, then do this:

flexSubset = function(sColumnToSubset, dMin)
                DT[get(sColumnToSubset) > dMin]

flexSubset("a", 0.5)

If you want to pass an unevaluated expression, then:

flexSubset = function(sColumnToSubset, dMin) {
                lhs = substitute(sColumnToSubset)
                DT[eval(lhs) > dMin]
             }

flexSubset(a, 0.5)
flexSubset(a / b, 0.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM