In data.table, one way to subset a table on the basis of a numerical vector of column numbers involves using with=FALSE
.
I'm trying to loop through a data.table on the basis of a numerical vector of column numbers, looking for rows meeting a certain criterion, as follows:
require(data.table)
ab=data.table(id=c("geneA", "geneB", "geneC", "geneA", "geneA", "geneB", "", "NA"),
co1=c(1,2,3,0,7), co2=c(0,0,4,5,6), nontarget=c(9,0,7,6,5),
co3=c(0,1,2,3,4))
target_col_nums=grep('co', colnames(ab))
##Data.table doesn't treat colnames(ab)[i] as one of the
## column name variables, and with=F only seems to work for j in dt[i,j,by]
for (i in target_col_nums){
print(ab[colnames(ab)[i]>3])
}
##This produces the desired output
ab[co1>3]
ab[co2>3]
ab[co3>3]
In my situation, my actual table is quite large, so I can't use the colnames themselves.
I hope that this is a useful question to the community.