I have dplyr::recode some factors, and I'm looking for a clean way to make a LaTeX table where new and old categories, i.e. levels, are compared.
Here is an illustration of problems using cyl from `mtcars. First a few packages,
# install.packages("tidyverse", "stargazer","reporttools") library(tidyverse)
and the data that I intend to use,
mcr <- mtcars %>% select(cyl) %>% as_tibble() mcr %>% print(n=5)
Now I create two new factors: one with three categories, cyl_3col and one with two, cyl_is_red , i.e.:
mcr_col <- mcr %>% as_tibble() %>% mutate(cyl_3col = factor(cyl, levels = c(4, 6, 8),labels = c("red", "blue", "green")), cyl_is_red = recode(cyl_3col, .default = 'is not red', 'red' = 'is red')) mcr_col %>% print(n=5)
Now I would like to show how the categories in cyl_3col and cyl_is_red .
Maybe something like this is better
#> cyl_is_red cyl_3col
maybe something like this, I imagine the is not red category spanning two lines with \multirow{} or something like that.
#> cyl_3col cyl_is_red
using stargazer or maybe some other teX. I am very open about how best to show transcoding. I guess there is some clever way to code this thoughtful by someone who is in front of me?
I used something like mcr_col %>% count(cyl_3col, cyl_is_red) , but I don't think it really works.