2015-04-21 45 views
1

我想排除df中具有JUST特定模式(AA,AB,AB,BB)的所有行。我的真實數據有超過20萬行和超過2K列!按照一個典型的輸入例如:排除R中只有特定模式的所有行

df <- "chr position sample21s sample23s sample22s 
    chr2 150  AB   BB  AA  
    chr4 250  A   AA  BB 
    chr5 350  AB   B  BB 
    chr7 550  AA   AA  AA 
    chr8 650  BB   BB  AB" 
df <- read.table(text=df, header=T) 

預期輸出:

chr position sample21s sample23s sample22s 
chr4 250  A   AA  BB 
chr5 350  AB   B  BB 

任何想法?

+0

你能提供一個可重複的例子:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

+0

完成。謝謝! – user2120870

+0

對不起 - 你想*排除* AA,AB和BB?在預期的輸出中顯示這些行仍然存在。 – verybadatthis

回答

1

這裏有一個替代...

> ind <- apply(df[, grepl("^sample", names(df))], 1, 
       function(x) sum(x %in% c("AA", "AB", "BB"))!=3) 

> df[ind, ] 
    chr position sample21s sample23s sample22s 
2 chr4  250   A  AA  BB 
3 chr5  350  AB   B  BB 
相關問題