I'm looking to process columns by criteria like class or common pattern matching via grep
.
My first attempt did not work:
require(data.table)
test.table <- data.table(a=1:10,ab=1:10,b=101:110)
##this does not work and hangs on my machine
test.table[,lapply(names(test.table)[grep("a",names(test.table))], get)]
Ricardo Saporta notes in an answer that you can use this construct, but you have to wrap get
in a dummy function:
##this works
test.table[,lapply(names(test.table)[grep("a",names(test.table))], function(x) get(x))]
Why do you need the anonymous function?
(The preferred/cleaner method is via .SDcols
:)
test.table[,.SD,.SDcols=grep("a",names(test.table))]
test.table[, grep("a", names(test.table), with = FALSE]