Seyma Kalay
Seyma Kalay

Reputation: 2861

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

Upvotes: 2

Views: 105

Answers (2)

PaulS
PaulS

Reputation: 25393

A possible and compact solution, based on purrr::map_dfr:

library(tidyverse)

map_dfr(df, ~ prop.table(table(.x)), .id = "id") %>% 
  column_to_rownames("id") %>% select(sort(names(.)))

#>           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

Another possible solution (not so compact), based on purrr::map and bind_rows:

library(tidyverse)

map(df, ~ prop.table(table(.x))) %>% bind_rows() %>% as.data.frame %>%
  `rownames<-`(names(df)) %>% select(sort(names(.)))

#>           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

Upvotes: 2

Ma&#235;l
Ma&#235;l

Reputation: 52319

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

Upvotes: 1

Related Questions