Search code examples
rdataframepurrrfrequency-table

Grouping columns and getting frequency using purrr::map


I am trying to get crosstabs of multiple columns using tidyverse code.

Sample data:

df <- data.frame(col1=c("a", "b", "c", "c"), col2=c(NA, "d", "d","e"))  %>%
  mutate_all(as.character)
df
  col1 col2
1    a <NA>
2    b    d
3    c    d
4    c    e

Using apply, I would do the following:

apply(df, 2, function(x) data.frame(table(x)))

I have tried the code below which does not work:

df %>% 
  map_df(function(.x) {
    group_by(.x) %>% summarise(n=n()) %>% print()
    })

Solution

  • In tidyverse you can do this in couple of ways.

    1. column-wise using purrr::map
    library(dplyr)
    purrr::map(names(df), ~df %>% count(.data[[.x]]))
    
    1. Get the data in long format and then count for each column and value
    df %>%
      tidyr::pivot_longer(cols = everything()) %>%
      count(name, value)