2012-12-20 30 views
0

我有一個例子文件如下:如何根據特定標準過濾行?

GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8 
g1 0.000 0.000 0.000 0.000 0.010 0.000 0.022 0.344 
g2 0.700 0.000 0.000 0.000 0.000 0.000 0.000 0.000 
g3 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 
g4 0.322 0.782 0.000 0.023 0.000 0.000 0.000 0.345 
g5 0.010 0.000 0.333 0.000 0.000 0.000 0.011 0.000 
g6 0.000 0.000 0.010 0.000 0.000 0.000 0.000 0.000 

我需要檢索的行(基因)的列表中,如果它有「2個或更多的樣品」同值「0.010或更多個」。所以我應該得到的結果列如下:

GENES 
g1 
g4 
g5 

任何人都可以幫助我嗎?

回答

6

這裏是一個可能的方式:

DF <- read.table(text= 
"GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8 
g1 0.000 0.000 0.000 0.000 0.010 0.000 0.022 0.344 
g2 0.700 0.000 0.000 0.000 0.000 0.000 0.000 0.000 
g3 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 
g4 0.322 0.782 0.000 0.023 0.000 0.000 0.000 0.345 
g5 0.010 0.000 0.333 0.000 0.000 0.000 0.011 0.000 
g6 0.000 0.000 0.010 0.000 0.000 0.000 0.000 0.000",header=T,sep=' ') 


rows <- sapply(1:nrow(DF),FUN=function(i){sum(DF[i,2:ncol(DF)] >= 0.01) >= 2}) 
subSet <- DF[rows,] 

> subSet 
    GENES Samp1 Samp2 Samp3 Samp4 Samp5 Samp6 Samp7 Samp8 
1 g1 0.000 0.000 0.000 0.000 0.01  0 0.022 0.344 
4 g4 0.322 0.782 0.000 0.023 0.00  0 0.000 0.345 
5 g5 0.010 0.000 0.333 0.000 0.00  0 0.011 0.000 

或類似這樣的:

subSet <- DF[apply(DF,1,function(x){sum(tail(x,-1) >= 0.01) >= 2}),] 

或本:

subSet <- DF[rowSums(DF[,2:ncol(DF)] >= 0.01) >= 2,] 

,你可以看到有很多方法來實現這一目標:)

+0

非常感謝..它的工作:) – Letin