使用 kableExtra 调整长桌

Adjust long tables with kableExtra

我正在使用 dslabs 包中的乳腺癌数据,我正在使用 kableExtra 来生成不错的 tables 但我有很多列,我试图缩小和当时生成 11-12 个变量的摘要 table,但看起来仍然太小。

问题 1:您知道我如何生成一个 table 行作为我的变量(列)和列与统计数据 minmaxmean ...?我相信有一种有效的方法可以做到这一点

# my current code

data(brca)
data=data.frame(cbind(brca$y,brca$x))
data=data %>% 
  rename(
    Diagnosis = V1
    )

desc=summary(data)


kable(desc[,1:12],caption = "Descriptif of variables",booktabs=T)%>%
  kable_styling(latex_options =c("striped", "scale_down"))

kable(desc[,13:21],caption = "Descriptif of variables",booktabs=T)%>%
  kable_styling(latex_options =c("striped", "scale_down"))

kable(desc[,22:31],caption = "Descriptif of variables",booktabs=T)%>%
  kable_styling(latex_options =c("striped", "scale_down"))

问题 2:如果您对如何在 latex 中包含 Rmd 文件有任何想法或资源,我们将不胜感激,因为现在,我用 R 生成 tables,然后截屏我的 tables,最后将它们插入我的 Latex(我有太多 tables 太累了)。

预先感谢您的帮助。

问题 1:

您可以使用 dplyr::summarize_all 汇总数据框中的所有列。这将输出一个包含一行的宽数据集,列数等于数据集中的列数乘以所需的汇总统计量。例如,它将包含 texture_mean_meantexture_mean_medtexture_mean_max.

tidyr::pivot_longer 会将这个宽数据集转换为您想要的更长的数据集。 names_tonames_pattern 就是这样做的。 (.*)_(.*)$ 是一个正则表达式,它捕获两件事:最后一个下划线之前的所有内容和最后一个下划线之后的所有内容:(texture_mean)_(mean)。第一次捕获映射到名为“variable”的列的值,第二次捕获成为具有相应值的新列的名称。

data %>% 
  dplyr::summarize_all(list(mean = ~mean(.),
                            med = ~median(.),
                            max  = ~max(.))) %>% 
  tidyr::pivot_longer(everything(),
                      names_to = c("variable", ".value"),
                      names_pattern = "(.*)_(.*)$") 

问题2:

查看 Hmisc::latex 函数。它将乳胶代码输出到文件:

data %>% 
  dplyr::summarize_all(list(mean = ~mean(.))) %>% 
  tidyr::pivot_longer(everything(),
                      names_to = c("variable", ".value"),
                      names_pattern = "(.*)_(.*)$") %>% 
    Hmisc::latex(na.blank = TRUE, 
                 booktabs = TRUE, 
                 table.env = FALSE, 
                 center = "none", 
                 file = "", 
                 title = "")

会输出

%latex.default(., na.blank = TRUE, booktabs = TRUE, table.env = FALSE,     center = "none", file = "", title = "")%
\begin{tabular}{llrrr}
\toprule
\multicolumn{1}{l}{}&\multicolumn{1}{c}{variable}&\multicolumn{1}{c}{mean}&\multicolumn{1}{c}{med}&\multicolumn{1}{c}{max}\tabularnewline
\midrule
1&Diagnosis&.37258347978910e+00$&.000e+00$&.000e+00$\tabularnewline
2&radius_mean&.41272917398946e+01$&.337e+01$&.811e+01$\tabularnewline
3&texture_mean&.92896485061511e+01$&.884e+01$&.928e+01$\tabularnewline
4&perimeter_mean&.19690333919156e+01$&.624e+01$&.885e+02$\tabularnewline
5&area_mean&.54889103690685e+02$&.511e+02$&.501e+03$\tabularnewline
6&smoothness_mean&.63602811950791e-02$&.587e-02$&.634e-01$\tabularnewline
7&compactness_mean&.04340984182777e-01$&.263e-02$&.454e-01$\tabularnewline
8&concavity_mean&.87993158172232e-02$&.154e-02$&.268e-01$\tabularnewline
9&concave_pts_mean&.89191458699473e-02$&.350e-02$&.012e-01$\tabularnewline
10&symmetry_mean&.81161862917399e-01$&.792e-01$&.040e-01$\tabularnewline
11&fractal_dim_mean&.27976098418278e-02$&.154e-02$&.744e-02$\tabularnewline
12&radius_se&.05172056239016e-01$&.242e-01$&.873e+00$\tabularnewline
13&texture_se&.21685342706503e+00$&.108e+00$&.885e+00$\tabularnewline
14&perimeter_se&.86605922671353e+00$&.287e+00$&.198e+01$\tabularnewline
15&area_se&.03370790861160e+01$&.453e+01$&.422e+02$\tabularnewline
16&smoothness_se&.04097891036907e-03$&.380e-03$&.113e-02$\tabularnewline
17&compactness_se&.54781388400703e-02$&.045e-02$&.354e-01$\tabularnewline
18&concavity_se&.18937163444640e-02$&.589e-02$&.960e-01$\tabularnewline
19&concave_pts_se&.17961370826011e-02$&.093e-02$&.279e-02$\tabularnewline
20&symmetry_se&.05422987697715e-02$&.873e-02$&.895e-02$\tabularnewline
21&fractal_dim_se&.79490386643234e-03$&.187e-03$&.984e-02$\tabularnewline
22&radius_worst&.62691898066784e+01$&.497e+01$&.604e+01$\tabularnewline
23&texture_worst&.56772231985940e+01$&.541e+01$&.954e+01$\tabularnewline
24&perimeter_worst&.07261212653779e+02$&.766e+01$&.512e+02$\tabularnewline
25&area_worst&.80583128295255e+02$&.865e+02$&.254e+03$\tabularnewline
26&smoothness_worst&.32368594024605e-01$&.313e-01$&.226e-01$\tabularnewline
27&compactness_worst&.54265043936731e-01$&.119e-01$&.058e+00$\tabularnewline
28&concavity_worst&.72188483304042e-01$&.267e-01$&.252e+00$\tabularnewline
29&concave_pts_worst&.14606223198594e-01$&.993e-02$&.910e-01$\tabularnewline
30&symmetry_worst&.90075571177504e-01$&.822e-01$&.638e-01$\tabularnewline
31&fractal_dim_worst&.39458172231986e-02$&.004e-02$&.075e-01$\tabularnewline
\bottomrule
\end{tabular}

有关更多信息,请查看此 question(特别是使用 latex 函数的答案)