2016-05-01 29 views
3

我有一個矩陣(稱爲結果)行,看起來像這樣找到並刪除其具有大於5%缺失數據

 id1 id2 id3 id4 id5 id6 id7 id8 id9 
snp1 1 2 0 NA 1 1 1 2 1 
snp2 2 2 2 2 0 2 NA NA 0 
snp3 NA NA 1 NA 0 NA NA 2 2 

到目前爲止,我已刪除的行和列被完全填滿使用NAS

indexsnp=apply(results,1, 
function(x) length(which(is.na(x)==T))) 
indexsnp=which(indexsnp==length(results[1,])) 
indexsample=apply(results,2, 
function(x) length(which(is.na(x)==T))) 
indexsample=which(indexsample==length(results[,1])) 

#get rid of indexes 
results=results[-indexsnp,] 
results=results[,-indexsample] 

我還有很多的NAS在我的數據集,所以現在我想看看哪些SNP低於95%的拆借利率(即該行包括5%以上NAS),然後刪除那些行。我不知道如何做到這一點。我曾嘗試

snpsum.col <- col.summary(results) 
library(snpStats) 
call <- 0.95 
use <- with(snpsum.col, (!is.na(Call.rate) & Call.rate >= call)) 
use[is.na(use)] <- FALSE    
cat(ncol(results)-sum(use),"SNPs will be removed due to low call 
rate.\n") 
genotype <- genotype[,use] 
snpsum.col <- snpsum.col[use,] 

,但我得到的錯誤

Error in col.summary(results) : not a SnpMatrix object 

有另一種方法,我可以做到這一點?

+1

這不只是'結果[rowSums(is.na(結果))<(NcoI位(結果)* 0.05),]'?用於移除完全填充「NA」的行的代碼也非常低效且不必要。只需使用'na.omit'或'complete.cases' –

+0

@DavidArenburg是否有辦法查看哪些行(或只是多少)已被刪除,如果我這樣做? –

+3

'which(rowSums(is.na(results))>(ncol(results)* .05))''。或者如果你想知道「有多少」,可以把它包裝成「sum」。並看到[this](http://stackoverflow.com/questions/4862178/remove-rows-with-nas-in-data-frame)問題的第一部分 –

回答

1

m如果是這樣的矩陣,執行

m <- m[is.na(m)%*%rep(1,ncol(m))<=ncol(m)*0.05,] 
相關問題