如何简单地用NA计算行数-R [英] How to simply count number of rows with NAs - R
问题描述
我试图计算具有整个df的NA的行数,因为我想计算具有df的总行数中具有NA的行的百分比.
I'm trying to compute the number of rows with NA of the whole df as I'm looking to compute the % of rows with NA over the total number of rows of the df.
我已经看过这篇文章:确定具有NA的行数,但它只显示特定范围的列.
I have already have seen this post: Determine the number of rows with NAs but it just shows a specific range of columns.
推荐答案
tl; dr:行明智的,您将需要sum(!complete.cases(DF))
或等效的sum(apply(DF, 1, anyNA))
tl;dr: row wise, you'll want sum(!complete.cases(DF))
, or, equivalently, sum(apply(DF, 1, anyNA))
有多种方法可以查看数据框中NA
值的数量,比例或位置:
There are a number of different ways to look at the number, proportion or position of NA
values in a data frame:
其中大多数是从逻辑数据帧开始的,每个NA
都带有TRUE
,其他所有位置都带有FALSE
.对于基本数据集airquality
Most of these start with the logical data frame with TRUE
for every NA
, and FALSE
everywhere else. For the base dataset airquality
is.na(airquality)
此数据集中有44个NA
值
sum(is.na(airquality))
# [1] 44
您可以查看每行或每列NA
个值的总数:
You can look at the total number of NA
values per row or column:
head(rowSums(is.na(airquality)))
# [1] 0 0 0 0 2 1
colSums(is.na(airquality))
# Ozone Solar.R Wind Temp Month Day
37 7 0 0 0 0
您也可以使用anyNA()
代替is.na()
:
# by row
head(apply(airquality, 1, anyNA))
# [1] FALSE FALSE FALSE FALSE TRUE TRUE
sum(apply(airquality, 1, anyNA))
# [1] 42
# by column
head(apply(airquality, 2, anyNA))
# Ozone Solar.R Wind Temp Month Day
# TRUE TRUE FALSE FALSE FALSE FALSE
sum(apply(airquality, 2, anyNA))
# [1] 2
complete.cases()
可以使用,但只能逐行使用:
complete.cases()
can be used, but only row-wise:
sum(!complete.cases(airquality))
# [1] 42
这篇关于如何简单地用NA计算行数-R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!