Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count the absolute number and the percentage of NAs for every column in a dataframe

I have the dataframe below and I want to create a new dataframe based on this with 3 columns. The first will be named "Field" and will contain the column names of this dataframe (col1,col2,col3). The second will be named "Absolute" and will contain the absolute number of missing values of this column and the third will be named Percentage and will contain the percentage of the missing values of this column. The number of columns and rows in my real dataframe is bigger.

col1<-c("as","df",NA)
col2<-c("ds",NA,NA)
col3<-c(NA,NA,NA)
df<-data.frame(col1,col2,col3)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try,

data.frame(field = names(df), 
           Absolute = colSums(is.na(df)), 
           Percentage = 100 * (colSums(is.na(df)) / nrow(df)),
           row.names = seq(nrow(df)))

     field Absolute Percentage
   1  col1        1   33.33333
   2  col2        2   66.66667
   3  col3        3  100.00000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading