Home > Software engineering >  Count multiple columns in a data frame in one output table
Count multiple columns in a data frame in one output table

Time:09-27

I have some survey data and I want to get a summary count of multiple columns. So far I have worked out how to calculate each one individually and create a % column:

help_bk <- likemind %>% 
  count((help_book_1), name = "count") %>% 
  mutate((count/38)*100) %>% 
  rename("percent" = "(count/38) * 100")
help_bk

Which gives me this output:

Output table

I want to execute this for multiple columns in one go and get a summary table of all the variables. (Each variable has the same options 1-7 as this one)

Can anyone help enlighten me (pref in a tidyverse function)? TIA

CodePudding user response:

We may loop over the column names of interest, get the count and store it in a list

library(dplyr)
library(purrr)
nm1 <- names(likemind)[1:5] # for the first five columns in the dataset
lst1 <- map(nm1, ~ 
    likemind %>% 
      select(all_of(.x)) %>%
      count(across(1), name = "count") %>% 
     mutate((count/38)*100) %>% 
     rename("percent" = "(count/38) * 100")
    )

CodePudding user response:

You are probably looking for across()

likemind %>% 
  mutate(across(everything(),
                ~list(count = count(.x),
                 percent = count/38*100)
               )
         )

It is hard to get an exact answer without a proper reproducible example.

  • Related