Grouping columns and getting frequency using purrr::map

223 Views Asked by At

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()
    })
3

There are 3 best solutions below

0
Ronak Shah On BEST ANSWER

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)
0
M-- On

It can be done using purrr::map like below:

library(purrr)

map(df, ~as.data.frame(table(.x)))

#> $col1
#>   .x Freq
#> 1  a    1
#> 2  b    1
#> 3  c    2
#> 
#> $col2
#>   .x Freq
#> 1  d    2
#> 2  e    1
0
akrun On

An option with lapply

lapply(df, function(x) as.data.frame(table(x)))