重新编码重新编码之前和之后的级别概述

Eri*_*ail 5 latex r stargazer dplyr

我有dplyr::recode一些因素,我正在寻找一种干净的方式来制作LaTeX表,比较新旧类别,即水平.

以下是使用cyl`mtcars 的问题的说明.先是一些包裹,

# install.packages("tidyverse", "stargazer","reporttools") 
library(tidyverse) 
Run Code Online (Sandbox Code Playgroud)

以及我打算使用的数据,

mcr <- mtcars %>% select(cyl) %>% as_tibble() 
mcr %>% print(n=5)
#> # A tibble: 32 x 1
#>     cyl
#> * <dbl>
#> 1  6.00
#> 2  6.00
#> 3  4.00
#> 4  6.00
#> 5  8.00
#> # ... with 27 more rows
Run Code Online (Sandbox Code Playgroud)

现在,我创建了两个新因子,一个有三个类别,cyl_3col一个有两个cyl_is_red,即:

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)
#> # A tibble: 32 x 3
#>     cyl cyl_3col cyl_is_red
#>   <dbl> <fct>    <fct>     
#> 1  6.00 blue     is not red
#> 2  6.00 blue     is not red
#> 3  4.00 red      is red    
#> 4  6.00 blue     is not red
#> 5  8.00 green    is not red
#> # ... with 27 more rows
Run Code Online (Sandbox Code Playgroud)

现在,我想展示如何分类cyl_3colcyl_is_red相关的类别.

也许这样的事情更好,

#> cyl_is_red  cyl_3col 
#> is red               
#>             red      
#> is not red           
#>             blue     
#>             green    
Run Code Online (Sandbox Code Playgroud)

可能这样的东西,我想象的is not red类别跨越两行\multirow{}或类似的东西.

#>  cyl_3col   cyl_is_red
#> 1 red       is red    
#> 2 blue      is not red
#> 3 green     ----------
Run Code Online (Sandbox Code Playgroud)

使用或可能的其他TeX工具.我对如何最好地显示重新编码非常开放.我假设有一些聪明的方法来编写这个想法来自我之前的人?

mcr_col %>% count(cyl_3col, cyl_is_red)现在用过类似的东西,但我认为它并没有真正起作用.

Ben*_*min 2

pixiedust有一个合并选项。

---
title: "Untitled"
output: pdf_document
header-includes: 
- \usepackage{amssymb} 
- \usepackage{arydshln} 
- \usepackage{caption} 
- \usepackage{graphicx} 
- \usepackage{hhline} 
- \usepackage{longtable} 
- \usepackage{multirow} 
- \usepackage[dvipsnames,table]{xcolor} 
---

```{r}
library(pixiedust)
library(dplyr)

mcr <- mtcars %>% select(cyl) %>% as_tibble() 
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 %>% 
  count(cyl_3col, cyl_is_red) %>% 
  select(-n) %>% 
  dust(float = FALSE) %>% 
  sprinkle(cols = "cyl_is_red",
           rows = 2:3,
           merge = TRUE) %>% 
  sprinkle(sanitize = TRUE,
           part = "head")
```
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述