Home > front end >  How to obtain freqs & percentages for all categorical vars in df
How to obtain freqs & percentages for all categorical vars in df

Time:09-27

My df, Chap3, has ~50 categorical variables. I want to produce a frequency table for each categorical variable that also includes percentages. The code below works fine for the single var bsex but I cannot figure out how to repeat it for all categorical vars. Have tried using variants of apply, using select_if(is.factor), etc, to no avail. Thank you for any advice.

    Chap3 %>% 
        count(bsex) %>% 
        mutate(percent = round(n / sum(n) * 100,1))

CodePudding user response:

For such cases it is better if you get the categorical data in long format.

library(dplyr)
library(tidyr)

Chap3 %>%
  pivot_longer(cols = where(is.factor)) %>%
  count(name, value) %>%
  group_by(name) %>%
  mutate(n = round(prop.table(n), 1)) %>%
  ungroup

#   name  value   n
#  <chr> <fct> <dbl>
#1 bsex      0   0.4
#2 bsex      1   0.6
#3 csex      0   0.5
#4 csex      1   0.5

data

It is easier to help if you provide data in a reproducible format

set.seed(123)
Chap3 <- data.frame(id = 1:10, 
                    bsex = factor(sample(c(1, 0), 10, replace = TRUE)), 
                    csex = factor(sample(c(1, 0), 10, replace = TRUE)))
  • Related