rlatex

How to create and export a Matrix (after analysis) as a Table using r and Latex?


I have finished my analysis. And my final output is matrix even with row and colomn names. Now I just need to make a nice table with lines and output it as (may be a pdf) or (may be like an editable word file).

I did my own research and saw that there are many packages like xtable and knitr. But when i install those packages and execute a code. I just get bunch of codes as Output.

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")

When i use kable, i get a table but i dont get the formatted table (like you can make in words). When i use xtable, i just get bunch of codes as output.

I feel like i am missing something very simple here. Maybe i need to add Latex to my R? so that when i run the code i will get table instead of code as an output.

I would appreciate if someone could nudge me in the right direction. Or any simple solution would be helpful too.


Solution

  • I do not know a simple way to format only a matrix or data.frame into a table as PDF or similar, like you can do for plots. However, You can easily use R Markdown to create the entire report including tables and plots with embedded R code. This way you don't have to bother with exporting plots or tables in the first place. The following example extends the default Rmd file created by RStudio with your code:

    ---
    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)
    ```
    

    You can convert this file to different output formats (HTML, PDF, DOCX) via the "Knit" button in RStudio or the rmarkdown::render function. Prerequisites: