【问题标题】:find and delete rows which have greater than 5% missing data查找并删除缺失数据大于 5% 的行
【发布时间】:2016-08-26 04:28:09
【问题描述】:

我有一个看起来像这样的矩阵(称为结果)

     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

到目前为止,我已经删除了使用 NA 完全填充的行和列

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]

我的数据集中仍然有很多 NA,所以现在我想查看哪些 snp 的调用率低于 95%(即哪些行包含超过 5% 的 NA),然后删除这些行。我不知道该怎么做。我试过了

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

还有其他方法可以做到这一点吗?

【问题讨论】:

  • 这不就是results[rowSums(is.na(results)) &lt; (ncol(results) * .05), ]吗?您用于删除完全由NA 填充的行的代码也非常低效且不必要。只需使用na.omitcomplete.cases
  • @DavidArenburg 如果我这样做,有没有办法查看哪些行(或多少行)已被删除?
  • which(rowSums(is.na(results)) &gt; (ncol(results) * .05))。如果您想知道“有多少”,或者将其包装成sum。有关问题的第一部分,请参阅this
  • 谢谢@DavidArenburg。所以只是澄清一下,我知道这非常愚蠢,但我对 R 很陌生,所以请多多包涵,我只是想确保我理解。如果我执行 newresults = results[rowSums(is.na(results)) (ncol(results) * .05)) 我得到的答案是我刚刚删除了多少行?
  • 顺便说一句,如果您删除行,您也可以跳过第一部分。因为如果你删除了至少有 5% 缺失数据的所有行,那么显然也将删除所有缺失数据的行。因此,您可以在一行中完成所有事情,而不是上面写的整个过程。

标签: r dataframe genetics


【解决方案1】:

如果m是这样一个矩阵,做

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 2021-06-25
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 2020-11-05
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多