I have tibble where col1
is a list of character vectors of variable length and col2
is a numeric vector indicating a group assignment, either 1 or 0. I want to first convert all of the character vectors in the list (col1
) to factors, and then unify all of the factors levels across these factors so that I can ultimately get a tally of counts for each factor level. For the example data below, that would mean the tally would be as follows:
overall:
level, count
"a", 2
"b", 2
"c", 2
"d", 3
"e", 1
for group=1:
level, count
"a", 1
"b", 2
"c", 1
"d", 1
"e", 0
for group=0:
level, count
"a", 1
"b", 0
"c", 1
"d", 2
"e", 1
The ultimate goal is to be able to get a total count of each factor level c("a","b","c","d","e")
and plot them by the grouping variable.
Here is some code that might give better context to my problem:
library(forcats)
library(purrr)
library(dplyr)
library(ggplot2)
tib <- tibble(col1=list(c("a","b"),
c("b","c","d"),
c("a","d","e"),
c("c","d")),
col2=c(1,1,0,0))
tib %>%
mutate(col3=map(.$col1,.f = as_factor)) %>%
mutate(col4=map(.$col3,.f = fct_unify))
Unfortunately, this code fails. I get the following error, but don't know why:
Error:
fsmust be a list
I thought my input was a list?
I appreciate any help anyone might offer. Thanks.