3 Tables

The insertion of tables in the text can be performed with the aid of the kable and kable extra and xtable package, or by writing the latex code directly in your file with LyX assistance. I will not define what is the best package for building tables. I believe that the choice are personal.

3.0.1 Package kable

My kable table (3.1)

Table 3.1: My table using kable package
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
My kable table (\@ref(kabletable))

``{r, message=FALSE}
library(kableExtra)

dat <- mtcars[1:5, 1:6]
kab <- knitr::kable(dat, caption="My table using kable package",
                         booktabs=T, label="kabletable")
kable_classic_2(kab, full_width=F, latex_options="hold_position")
```

See references for other examples.

3.0.2 Package xtable

My table in xtable (??)

My table using xtable package
mpg cyl disp hp drat wt
Mazda RX4 21.00 6.00 160.00 110.00 3.90 2.62
Mazda RX4 Wag 21.00 6.00 160.00 110.00 3.90 2.88
Datsun 710 22.80 4.00 108.00 93.00 3.85 2.32
Hornet 4 Drive 21.40 6.00 258.00 110.00 3.08 3.21
Hornet Sportabout 18.70 8.00 360.00 175.00 3.15 3.44

\

For a PDF file, change type=“html” to type=“latex.”

My table in xtable (\@ref(xtabletab))

``{r, results='asis', message=FALSE, warning=FALSE}
library(xtable)

print(xtable(dat, caption="My table using xtable package", label="xtabletab"), comment=FALSE,
            caption.placement="top", type="html")
```

See references for other examples.

3.0.3 LyX for table

You can insert Latex code directly into your R markdown code. The LyX code must be within the table environment.

LyX table code

Figure 3.1: LyX table code

My LyX table (3.2)

LyX table

Figure 3.2: LyX table


My LyX table (\@ref(lyxtab2))

\begin{table}[!h]
\centering
\caption{My LyX table}
\begin{tabular}{ccccccc}
\hline 
Car & mpg & cyl & disp & hp & drat & wt\tabularnewline
\hline 
Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62\tabularnewline
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88\tabularnewline
Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32\tabularnewline
Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21\tabularnewline
Hornet Sportabout & 18.70 & 8.00 & 306.00 & 175.00 & 3.15 & 3.44\tabularnewline
\hline 
\end{tabular}
\label{lyxtab2}
\end{table}