I am looking to process columns by criteria such as class or general pattern matching with grep.
My first attempt did not work:
require(data.table)
test.table <- data.table(a=1:10,ab=1:10,b=101:110)
test.table[,lapply(names(test.table)[grep("a",names(test.table))], get)]
Ricardo Saporta notes in the answer that you can use this construct, but you should wrap getin a dummy function:
test.table[,lapply(names(test.table)[grep("a",names(test.table))], function(x) get(x))]
Why do you need an anonymous function?
(Preferred / cleaning method is done through .SDcols:)
test.table[,.SD,.SDcols=grep("a",names(test.table))]
test.table[, grep("a", names(test.table), with = FALSE]
source
share