MP61
MP61

Reputation: 169

Calculate percentage of missing string variables in each column

I have a data frame with some columns which are strings with missing values. Is there a way (using dplyr) to efficiently calculate the percentage of each column that is missing i.e. "". So I dont have to calculate each column percentage missing individually ?

I have tried the following but dosnt seem to work

library(dplyr) 
#Create data frame

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

x %>% 
summarise_each(funs(100*mean(is.null(.))))

#Result is
#a b c
#0 0 0

Want to get something like

#a    b c
#0.75 0 0.50

Upvotes: 1

Views: 583

Answers (1)

jeremycg
jeremycg

Reputation: 24945

in base:

colSums(x!="")/nrow(x)
   a    b    c 
0.75 1.00 0.50 

Upvotes: 0

Related Questions