Incorporate data frame into Latex

Hello,

How can we incorporate data frame into Latex table as example below, with the values coming from
the data frame ?

output: pdf_document
header-includes:
- \usepackage{caption}
- \usepackage{multirow}
- \usepackage{setspace}
---

\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|  }
 \hline
 \multicolumn{4}{|c|}{Country List} \\
 \hline
 Country & ISO ALPHA&ISO ALPHA&ISO numeric\\
 \hline
 Afghan& AF    &AFG&   004\\
 Aland &   AX  & ALA   &248\\
 Albania &AL & ALB&  008\\
 Algeria    &DZ & DZA&  012\\
 American Samoa&   AS  & ASM&016\\
 Andorra& AD  & AND   &020\\
 Angola& AO  & AGO&024\\
 \hline
\end{tabular}

Before answering your question, I'd like to request you not to paste your code withing block quotes. Please paste your code withing a pair of triple back ticks for normal R code, and for Rmd codes, use 4 back ticks.

What's wrong with your code? It creates the table with no problem, though you haven't used caption or setspace anywhere, as far as I understand. multirow is also unnecessary, as Country List is, in my opinion, more suitable for a table caption, not for a different row of table.

I'm not sure, but does this mean that you've a data frame in R and wish to include it in a document as a table? You can use knitr::kable. Here's an example, but for more control, check the documentation.

---
output: pdf_document
header-includes:
- \usepackage{multirow}
---

# the way you did it

\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|  }
 \hline
 \multicolumn{4}{|c|}{Country List} \\
 \hline
 Country & ISO ALPHA&ISO ALPHA&ISO numeric\\
 \hline
 Afghan& AF    &AFG&   004\\
 Aland &   AX  & ALA   &248\\
 Albania &AL & ALB&  008\\
 Algeria    &DZ & DZA&  012\\
 American Samoa&   AS  & ASM&016\\
 Andorra& AD  & AND   &020\\
 Angola& AO  & AGO&024\\
 \hline
\end{tabular}

# in case you already have the data frame in R

```{r}
df <- data.frame(stringsAsFactors = FALSE,
                 Country = c("Afghan", "Aland", "Albania", "Algeria", "American Samoa", "Andorra", "Angola"),
                 ISO.ALPHA = c("AF", "AX", "AL", "DZ", "AS", "AD", "AO"),
                 ISO.ALPHA = c("AFG", "ALA", "ALB", "DZA", "ASM", "AND", "AGO"),
                 ISO.numeric = c(4, 248, 8, 12, 16, 20, 24))
knitr::kable(x = df,
             caption = "Country List")
```

The above code generates try.pdf (104.7 KB).

Since you're repeated asking questions regarding tables, I'll suggest you to look at the kableExtra package. I've hear of another package gt, but I haven't used it myself.

@Yarnabrina

Great thanks for the answer, what you did isn't that not Latex but seems to be more like R with kableExtra ? What if I wanted better formatting and also color, and borders, thick, and thin, and also font size and color etc.

I do think that kablExtra is worth giving a try. It can be used with LaTeX, as you can find here:

And, if your main focus is LaTeX, the following is probably a better place to ask those questions:

Anyway, I didn't really understand what you want. For people with more expertise in LaTeX than me, perhaps a desired format of your table will be helpful.

Good luck!

@Yarnabrina,

no problem. Thanks !

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.