简体   繁体   English

使用dplyr计算,每列中NA的百分比

[英]Calculate using dplyr, percentage of NA'S in each column

I have a data frame with some columns with missing values.我有一个数据框,其中一些列缺少值。 Is there a way (using dplyr) to efficiently calculate the percentage of each column that is missing ie NA.有没有办法(使用 dplyr)有效地计算每列丢失的百分比,即 NA。 Sought of like a colSum equivalent.寻求像 colSum 等价物。 So I dont have to calculate each column percentage missing individually ?所以我不必单独计算每列丢失的百分比?

First, I created a test data for you:首先,我为您创建了一个测试数据:

a<- c(1,NA,NA,4)
b<- c(NA,2,3,4)
x<- data.frame(a,b)
x
#    a  b
# 1  1 NA
# 2 NA  2
# 3 NA  3
# 4  4  4

Then you can use colMeans(is.na(x)) :然后你可以使用colMeans(is.na(x))

colMeans(is.na(x))
#    a    b 
# 0.50 0.25 

We can use summarise_each我们可以使用summarise_each

 library(dplyr)
 x %>% 
   summarise_each(funs(100*mean(is.na(.))))

喜欢这种简洁的purrr::map类型:

x %>% map(~ mean(is.na(.)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM