I find purrr SUPER useful for making results tables for a bunch of different variables. I was wondering if there was a way for the unnest() function (or otherwise) to expand a high order variable into blanks, rather than just repeating.
For example, with this code:
library(tidyverse)
data <- data.frame(
group1 = sample(c('dog','cat', 'gecko'), 100, replace = T),
group2 = sample(c('hot dog', 'not hotdog', 'other'), 100, replace = T)
)
my_freq <- function(var){
result <- as.data.frame(table(data[[var]]))
colnames(result) <- c('level', 'n')
return(result)
}
the_table <- data.frame(var = c('group1', 'group2'))
the_table <- the_table %>%
mutate(
result = map(var, my_freq)
) %>%
unnest(result)
Instead of the resulting table looking like:
It would look like this:
I guess this would be a multi-level index in python, but not sure how to accomplish in r.

