报告data.frame中缺失值的优雅方法 [英] Elegant way to report missing values in a data.frame
本文介绍了报告data.frame中缺失值的优雅方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我写的一小段代码,用于报告数据框中缺少值的变量.我正在尝试一种更优雅的方式来执行此操作,该方法可能返回一个data.frame,但我遇到了麻烦:
Here's a little piece of code I wrote to report variables with missing values from a data frame. I'm trying to think of a more elegant way to do this, one that perhaps returns a data.frame, but I'm stuck:
for (Var in names(airquality)) {
missing <- sum(is.na(airquality[,Var]))
if (missing > 0) {
print(c(Var,missing))
}
}
我正在处理带有数十到数百个变量的data.frames,因此关键是我们只报告缺少值的变量.
I'm dealing with data.frames with dozens to hundreds of variables, so it's key that we only report variables with missing values.
推荐答案
只需使用sapply
> sapply(airquality, function(x) sum(is.na(x)))
Ozone Solar.R Wind Temp Month Day
37 7 0 0 0 0
您还可以在is.na()
> apply(is.na(airquality),2,sum)
Ozone Solar.R Wind Temp Month Day
37 7 0 0 0 0
> colSums(is.na(airquality))
Ozone Solar.R Wind Temp Month Day
37 7 0 0 0 0
这篇关于报告data.frame中缺失值的优雅方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文