Calculate using dplyr, percentage of NA'S in e

2020-06-21 03:42发布

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 i.e. NA. Sought of like a colSum equivalent. So I dont have to calculate each column percentage missing individually ?

标签: r dplyr
3条回答
成全新的幸福
2楼-- · 2020-06-21 04:19

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))
#    a    b 
# 0.50 0.25 
查看更多
Juvenile、少年°
3楼-- · 2020-06-21 04:27

We can use summarise_each

 library(dplyr)
 x %>% 
   summarise_each(funs(100*mean(is.na(.))))
查看更多
家丑人穷心不美
4楼-- · 2020-06-21 04:30

Loving the concision of purrr::map for this type of thing:

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

查看更多
登录 后发表回答