如何以最小的格式将数据框导出到乳胶? [英] how to export a dataframe to latex with some minimal formatting?

查看:61
本文介绍了如何以最小的格式将数据框导出到乳胶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单的例子

 库(dplyr)dataframe<-data_frame(mytext1 = c('HELLO','世界'),mytext2 = c('HELLO','再次'),值1 = c(1,2),值2 = c(1,2))#小动作:2 x 4mytext1 mytext2 value1 value2< chr>< chr>< dbl>< dbl>1 HELLO HELLO 1 12世界再次2 2 

我想将此数据帧导出到乳胶表中,但要进行一些(但很重要)的调整.

尤其是,我想在乳胶输出中添加一个超级列,该超级列分隔文本变量,另一个分隔数字变量.也就是说,此图片中体现了 mat com 精神的东西:

我尝试了R包 xtable tables ,但是我无法实现某些目标(可能是因为它们创建了摘要统计表).我在这里想念什么吗?

谢谢!

解决方案

您可以使用 knitr 包中的 kable 加上 kableExtra 包裹:

 `{r表,results ='asis'}图书馆(针织)图书馆(kableExtra)图书馆(magrittr)dataframe<-data.frame(mytext1 = c('HELLO','世界'),mytext2 = c('HELLO','再次'),值1 = c(1,2),值2 = c(1,2))数据框%&%;%kable(format ='latex',booktabs = TRUE)%>%add_header_above(header = c("Text" = 2,"Values" = 2))``` 

在这里,我展示了RMarkdown块中包含的代码,编织到PDF时该表会自动包含在其中,但是代码也应该在RMarkdown之外单独工作.

单独运行代码,您将获得表的Latex代码作为输出:

  \ begin {tabular} {llrr}\ toprule\ multicolumn {2} {c} {Text}&\ multicolumn {2} {c} {值} \\\cmidrule(l{2pt}r{2pt}){1-2} \cmidrule(l{2pt}r{2pt}){3-4}mytext1&mytext2&值1和值2\ midrule你好你好1&1 \\世界与再来一次2&2 \\\ bottomrule\ end {表格} 

Consider this simple example

library(dplyr)
dataframe <- data_frame(mytext1 = c('HELLO',
                                   'WORLD'),
                        mytext2 = c('HELLO',
                                   'AGAIN'),
                        value1 = c(1,2), 
                        value2 = c(1,2))

# A tibble: 2 x 4
  mytext1 mytext2 value1 value2
    <chr>   <chr>  <dbl>  <dbl>
1   HELLO   HELLO      1      1
2   WORLD   AGAIN      2      2

I would like to export this dataframe to a latex table, but with some little (but important) tweaks.

In particular, I would like, in the latex output, to add a supercolumn that separates the text variables and another that separates the numeric variables. That is, something in the spirit of mat and com in this image:

I tried the R packages xtable and tables but I was unable to achieve something close (likely because they create summary statistics tables). Am I missing something here?

Thanks!

解决方案

You can do this using kable from the knitr package plus the kableExtra package:

```{r table, results='asis'}
library(knitr)
library(kableExtra)
library(magrittr)

dataframe <- data.frame(mytext1 = c('HELLO',
                                   'WORLD'),
                        mytext2 = c('HELLO',
                                   'AGAIN'),
                        value1 = c(1,2), 
                        value2 = c(1,2))

dataframe %>%
    kable(format = 'latex', booktabs = TRUE) %>%
    add_header_above(header = c("Text" = 2, "Values" = 2))
``` 

Here I've shown the code included in an RMarkdown chunk, where the table is automatically included when knitting to PDF, but the code should also work on its own outside of RMarkdown.

Running the code by itself you get the Latex code for the table as output:

\begin{tabular}{llrr}
\toprule
\multicolumn{2}{c}{Text} & \multicolumn{2}{c}{Values} \\
\cmidrule(l{2pt}r{2pt}){1-2} \cmidrule(l{2pt}r{2pt}){3-4}
mytext1 & mytext2 & value1 & value2\\
\midrule
HELLO & HELLO & 1 & 1\\
WORLD & AGAIN & 2 & 2\\
\bottomrule
\end{tabular}

这篇关于如何以最小的格式将数据框导出到乳胶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆