通过查询字符串过滤data.table

时间:2016-04-08 05:46:22

标签: r data.table dplyr

您有什么方法可以通过存储在字符中的查询来过滤data.table吗?对于。离。

m <- structure(list(Hugo_Symbol = c("ABCA10", "ABCA4", "ABCC3", "ABCG4", 
"ACTBL2", "ADAMTS12", "ADCY5", "ADSS", "AGXT2", "ANG"), Variant_Classification = c("Splice_Site", 
"Missense_Mutation", "Missense_Mutation", "Missense_Mutation", 
"Missense_Mutation", "Missense_Mutation", "Missense_Mutation", 
"Splice_Site", "Missense_Mutation", "Missense_Mutation"), Variant_Type = c("SNP", 
"SNP", "SNP", "SNP", "SNP", "SNP", "SNP", "SNP", "SNP", "SNP"
), Tumor_Sample_Barcode = c("TCGA.AB.2988", "TCGA.AB.2869", "TCGA.AB.2887", 
"TCGA.AB.2934", "TCGA.AB.2931", "TCGA.AB.2945", "TCGA.AB.2833", 
"TCGA.AB.2912", "TCGA.AB.2822", "TCGA.AB.2988")), .Names = c("Hugo_Symbol", 
"Variant_Classification", "Variant_Type", "Tumor_Sample_Barcode"
), row.names = c(NA, -10L), class = c("data.table", "data.frame"
))

过滤

#These work fine
m[Variant_Classification %in% 'Splice_Site']
dplyr::filter(.data = m, filter = Variant_Classification %in% 'Splice_Site')

现在如果查询存储在字符变量中该怎么办。

query <- "Variant_Classification %in% 'Splice_Site'"
#These don't work
dplyr::filter(.data = m, filter = query)
m[,query, with =F]

有没有办法这样做?

感谢。

1 个答案:

答案 0 :(得分:6)

你可以用:

dplyr

dplyr::filter(.data = m, filter = eval(parse(text=query))) 的解决方案应该是等效的:

var v:Number = 0;
function getLocation(a,b,c,d,e)
 {
     //find out previous clip, based on v variable;
    var ch:MovieClip = this.getChildByName("myline"+(v-1)) as MovieClip;
    if(ch)
    {
        ch.graphics.clear(); 
        //even remove it because seems like you dont need it any more
        this.removeChild(ch);
    }

    var line:MovieClip = new MovieClip();
    line.name = "myline"+v;//give it a name, help in finding later
    line.graphics.lineStyle(2,0x00CCFF);
    line.graphics.moveTo(a,b);
    if (v>0)
    {
        line.graphics.lineTo(c,d);
    }
    else
    {
        v++;
        line.graphics.lineTo(mouseX,mouseY);
     }
    this.addChild(line);
 }