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()
})
In tidyverse
you can do this in couple of ways.
purrr::map
library(dplyr)
purrr::map(names(df), ~df %>% count(.data[[.x]]))
df %>%
tidyr::pivot_longer(cols = everything()) %>%
count(name, value)