【问题标题】:How to create and export a Matrix (after analysis) as a Table using r and Latex?如何使用 r 和 Latex 创建矩阵(分析后)并将其导出为表格?
【发布时间】:2019-08-28 06:31:32
【问题描述】:

我已经完成了我的分析。我的最终输出是矩阵,即使有行和列名。 现在我只需要用线条制作一个漂亮的表格并将其输出为(可能是 pdf)或(可能像可编辑的 word 文件)。

我自己研究了一下,发现有很多包,比如 xtable 和 knitr。但是当我安装这些包并执行代码时。我只是得到一堆代码作为输出。

mat <- matrix(c(1:91), ncol = 7, byrow=F)
rownames(mat) <- c("Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8","Y9","Y10", 
"Y11", "Y12", "Total water")
colnames(mat) <- c("Runoff", "RM", "DEEP Percolation", "ET", 
  "Lateralflow", "Change in SW", "Change in FW")
   mat
library(knitr)
kable(mat)
library(xtable)

xtable(mat, type="latex")

当我使用 kable 时,我得到了一个表格,但我没有得到格式化的表格(就像你可以用文字制作的那样)。当我使用 xtable 时,我只是得到一堆代码作为输出。

我觉得我在这里遗漏了一些非常简单的东西。也许我需要将乳胶添加到我的 R 中?这样当我运行代码时,我会得到表格而不是代码作为输出。

如果有人能将我推向正确的方向,我将不胜感激。 或者任何简单的解决方案也会有所帮助。

【问题讨论】:

  • 如果你使用format = "html",它是否有效?
  • @heck1 不,即使我使用 format = "html" 我仍然会得到一堆代码作为输出
  • 那么你的矩阵可能有问题。你能提供一个可重现的例子吗?考虑如何做一个很好的例子:stackoverflow.com/questions/5963269/…,看看你如何相应地改变你的问题。
  • knitr::kable 主要用于 R Markdown 文件中。这使您可以轻松地将代码和散文放在一个可重现的文件中进行分析。但是,您也可以将 knitr::kable 生成的 LaTeX 代码保存在变量中,将其写入文件并将其包含在 tex 文件中。
  • @heck1 我刚刚编辑了问题以制作可重现的代码。数据很简单,但我的最终输出是相同的,只是数据不同。现在我想得到一个表格(就像我可以得到一个很好的绘图图像)作为输出。我不知道该怎么做。

标签: r datatables latex


【解决方案1】:

我不知道一种将矩阵或 data.frame 格式化为 PDF 或类似格式的表格的简单方法,就像你可以为绘图做的那样。但是,您可以轻松地使用 R Markdown 创建整个报告,包括带有嵌入式 R 代码的表格和绘图。这样一来,您就不必为导出绘图或表格而烦恼。以下示例使用您的代码扩展了 RStudio 创建的默认 Rmd 文件:

---
title: "My Analysis"
author: "Samrat"
date: "`r Sys.Date()`"
output:
  html_document: default
  pdf_document: default
  word_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

## Including Tables

If you have tabular data as `matrix` or `data.frame` you can output it in a nicely formatted way. First the "analysis" that produces the data:

```{r analysis}
mat <- matrix(c(1:91), ncol = 7, byrow=F)
rownames(mat) <- c("Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8","Y9","Y10", 
"Y11", "Y12", "Total water")
colnames(mat) <- c("Runoff", "RM", "DEEP Percolation", "ET", 
  "Lateralflow", "Change in SW", "Change in FW")
```

Now we print the data as a table:

```{r table, echo=FALSE}
library(knitr)
kable(mat)
```

您可以通过 RStudio 中的“Knit”按钮或rmarkdown::render 函数将此文件转换为不同的输出格式(HTML、PDF、DOCX)。先决条件:

  • rmarkdown 来自 CRAN 的包
  • pandoc 程序,RStudio 自带
  • 对于 PDF 输出,您需要一个 TeX 系统。在许多情况下,最简单的方法是来自 R 的tinytex::install_tinytex()

【讨论】:

  • 太棒了!这对我来说效果很好。非常感谢您的帮助。
  • @Samrat 欢迎您。如果有帮助,请考虑接受/支持答案。
  • 我会记住从下次开始这样做。谢谢你的提醒。
猜你喜欢
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多