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

proportion of factor values in R

I have a data set with a bunch of factor values. I am looking for a decent way to print out the percentage of each value. many thanks in advance.

mtcars; rownames(mtcars) <- NULL
df <- mtcars[,c(2,8,9)]
df$am <- factor(df$am); df$vs <- factor(df$vs); df$cyl <- factor(df$cyl)


sapply(df, function(x) if("factor" %in% class(x)) {prop.table(table(x))})

Expected Answer

     0    1     4    6    8 
cyl  NA  NA     0.34 0.21 0.43 
vs   0.56 0.43  NA   NA   NA
am   0.59 0.40  NA   NA   NA

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

>Solution :

You can use dplyr::bind_rows:

library(dplyr)
s <- sapply(df, function(x) if("factor" %in% class(x)) prop.table(table(x)))

bind_rows(s, .id = "col") %>% 
  relocate(col, order(colnames(.)))

## A tibble: 3 × 6
#  col   `0`     `1`     `4`     `6`     `8`    
#  <chr> <table> <table> <table> <table> <table>
#1 cyl        NA      NA 0.34375 0.21875 0.4375 
#2 vs    0.56250 0.43750      NA      NA     NA 
#3 am    0.59375 0.40625      NA      NA     NA 

or, with the rownames:

bind_rows(s) %>% 
  relocate(order(colnames(.))) %>% 
  as.data.frame() %>% 
  `rownames<-`(names(s))

#          0       1       4       6      8
#cyl      NA      NA 0.34375 0.21875 0.4375
#vs  0.56250 0.43750      NA      NA     NA
#am  0.59375 0.40625      NA      NA     NA
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