首页 > 代码库 > R语言移除缺失值 NA

R语言移除缺失值 NA


有三种方法  !is.na  , na.omit, complete.cases

> d <- read.table("GWAS_s2.qassoc", header=T, stringsAsFactors=F)  

// 文件行数
> nrow(d)
[1] 431493

> d1 <- subset(d, select=c("CHR", "SNP", "BP", "P"))  

// 计算非NA 的行数

> num.bool <- complete.cases(d1)
> head(num.bool)
[1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE  

> sum(num.bool) 
[1] 363836                                                                                                            
                                                                                                                            
> dn1 <- d1[which(!is.na(d1$P)),]                                                                                                                                                 
> nrow(dn1)                                                                                                                                                                       
[1] 363836

> dn2 <- na.omit(d1)
> nrow(dn2)                                                                                                                                                            
[1] 363836

> dn3 <-d1[complete.cases(d1[,4]),]                                                                                                                                               
> nrow(dn3)                                                                                                                                                                      
[1] 363836


> dn4 <-d1[complete.cases(d1),]                                                                                                                                                  
> nrow(dn4)
[1] 363836


方法三和方法四, 一个是根据第四列是否为NA判断的, 一个是根据所有列。

本文出自 “R和Python应用” 博客,请务必保留此出处http://matrix6ro.blog.51cto.com/1746429/1890622

R语言移除缺失值 NA