Hi All,
I would like to have my R dataframe nicely printed in pdf file :
combined_tab <- structure(list(
Likert_0 = c("21 (12.43%)", "77 (45.56%)", "26 (15.38%)", "148 (87.57%)", "30 (17.75%)", "39 (23.08%)", "26 (15.38%)", "15 (8.88%)", "30 (17.75%)", "44 (26.04%)", "31 (18.34%)", "125 (73.96%)", "52 (30.77%)", "42 (24.85%)", "57 (33.73%)", "83 (49.11%)"),
Likert_1 = c("14 (8.28%)", "35 (20.71%)", "41 (24.26%)", "5 (2.96%)", "42 (24.85%)", "27 (15.98%)", "19 (11.24%)", "35 (20.71%)", "37 (21.89%)", "41 (24.26%)", "25 (14.79%)", "24 (14.20%)", "32 (18.93%)", "21 (12.43%)", "26 (15.38%)", "27 (15.98%)"),
Likert_2 = c("31 (18.34%)", "12 (7.10%)", "46 (27.22%)", "5 (2.96%)", "39 (23.08%)", "28 (16.57%)", "31 (18.34%)", "38 (22.49%)", "44 (26.04%)", "27 (15.98%)", "39 (23.08%)", "4 (2.37%)", "29 (17.16%)", "28 (16.57%)", "24 (14.20%)", "24 (14.20%)"),
Likert_3 = c("24 (14.20%)", "19 (11.24%)", "21 (12.43%)", "8 (4.73%)", "36 (21.30%)", "33 (19.53%)", "35 (20.71%)", "48 (28.40%)", "32 (18.93%)", "35 (20.71%)", "38 (22.49%)", "11 (6.51%)", "34 (20.12%)", "47 (27.81%)", "31 (18.34%)", "16 (9.47%)"),
Likert_4 = c("79 (46.75%)", "26 (15.38%)", "35 (20.71%)", "3 (1.78%)", "22 (13.02%)", "42 (24.85%)", "58 (34.32%)", "33 (19.53%)", "26 (15.38%)", "22 (13.02%)", "36 (21.30%)", "5 (2.96%)", "22 (13.02%)", "31 (18.34%)", "31 (18.34%)", "19 (11.24%)")
), row.names = c(NA, -16L), class = "data.frame")
How do I do it, please ?
I have read this:
https://stackoverflow.com/questions/9274013/generating-latex-output-from-r-data-frame
and I have done this in R script:
latex_code <- "
\begin{table}[ht]
\centering
\begin{tabular}{rlllll}
\hline
& Likert\_0 & Likert\_1 & Likert\_2 & Likert\_3 & Likert\_4 \\
\hline
1 & 21 (12.43\%) & 14 (8.28\%) & 31 (18.34\%) & 24 (14.20\%) & 79 (46.75\%) \\
2 & 77 (45.56\%) & 35 (20.71\%) & 12 (7.10\%) & 19 (11.24\%) & 26 (15.38\%) \\
3 & 26 (15.38\%) & 41 (24.26\%) & 46 (27.22\%) & 21 (12.43\%) & 35 (20.71\%) \\
4 & 148 (87.57\%) & 5 (2.96\%) & 5 (2.96\%) & 8 (4.73\%) & 3 (1.78\%) \\
5 & 30 (17.75\%) & 42 (24.85\%) & 39 (23.08\%) & 36 (21.30\%) & 22 (13.02\%) \\
6 & 39 (23.08\%) & 27 (15.98\%) & 28 (16.57\%) & 33 (19.53\%) & 42 (24.85\%) \\
7 & 26 (15.38\%) & 19 (11.24\%) & 31 (18.34\%) & 35 (20.71\%) & 58 (34.32\%) \\
8 & 15 (8.88\%) & 35 (20.71\%) & 38 (22.49\%) & 48 (28.40\%) & 33 (19.53\%) \\
9 & 30 (17.75\%) & 37 (21.89\%) & 44 (26.04\%) & 32 (18.93\%) & 26 (15.38\%) \\
10 & 44 (26.04\%) & 41 (24.26\%) & 27 (15.98\%) & 35 (20.71\%) & 22 (13.02\%) \\
11 & 31 (18.34\%) & 25 (14.79\%) & 39 (23.08\%) & 38 (22.49\%) & 36 (21.30\%) \\
12 & 125 (73.96\%) & 24 (14.20\%) & 4 (2.37\%) & 11 (6.51\%) & 5 (2.96\%) \\
13 & 52 (30.77\%) & 32 (18.93\%) & 29 (17.16\%) & 34 (20.12\%) & 22 (13.02\%) \\
14 & 42 (24.85\%) & 21 (12.43\%) & 28 (16.57\%) & 47 (27.81\%) & 31 (18.34\%) \\
15 & 57 (33.73\%) & 26 (15.38\%) & 24 (14.20\%) & 31 (18.34\%) & 31 (18.34\%) \\
16 & 83 (49.11\%) & 27 (15.98\%) & 24 (14.20\%) & 16 (9.47\%) & 19 (11.24\%) \\
\hline
\end{tabular}
\end{table}
"
# Save the LaTeX code to a .tex file
writeLines(latex_code, con = "example.tex")
# Compile the LaTeX file into a PDF
tinytex::latexmk("example.tex")
but I am getting errors:
! LaTeX Error: Environment table undefined.
Error: LaTeX failed to compile example.
How to correct this to have that dataframe nicely saved in pdf or docx file with this nice Latex output and font.
Latex code I obtained from:
xtable(combined_tab)