r - 查找并删除缺失数据大于 5% 的行

标签 r dataframe genetics

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

     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

还有其他方法吗?

最佳答案

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

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

关于r - 查找并删除缺失数据大于 5% 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36964957/

相关文章:

r - 计数大于特定值的观察值

Python DataFrame循环和切片问题

r - 如何在R中读取vcf文件

python - 创建字典的子集,按值列表对原始字典进行排序。

r - 清除 RStudio 中所有绘图的代码

r - 如何将 geom_text 标签对齐到躲避的 geom_col 上方

r - 在 Lavaan 生长曲线模型中提取个体轨迹

python - 使用 Pandas 进行多列分组以找到每组的最大值

python - 从pandas数据框中同时获取最大值和具有最大值的索引

python - 生成PCA加载矩阵时如何将pandas dataframe列设置为索引