本文介绍了如何找到数据帧中NA的百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找列中以及整个数据帧中NA的百分比:

I am trying to find the percentage of NAs in columns as well as inside the whole dataframe:

我评论过的第一种方法给我零,而未评论过的第二种方法给我矩阵.不知道我在想什么.任何提示都非常感谢!

The first method which I have commented gives me zero and the second method which is not commented gives me a matrix. Not sure what I am missing. Any hint is truly appreciated!

cp.2006<-read.csv(file="cp2006.csv",head=TRUE)

#countNAs <- function(x) { 
#  sum(is.na(x)) 
#} 
#total=0
#for (i in col(cp.2006)) {
#  total=countNAs(i)+total
#}
#print(total)
count<-apply(cp.2006, 1, function(x) sum(is.na(x)))
dims<-dim(cp.2006)
num<-dims[1]*dims[2]
NApercentage<-(count/num) * 100
print(NApercentage)

推荐答案

x = data.frame(x = c(1, 2, NA, 3), y = c(NA, NA, 4, 5))

对于整个数据框:

sum(is.na(x))/prod(dim(x))

mean(is.na(x))

对于列:

apply(x, 2, function(col)sum(is.na(col))/length(col))

colMeans(is.na(x))

这篇关于如何找到数据帧中NA的百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:47