3

I have a long table;

long_table <- structure(list(species = c("ABIBAL", "ACEPEN", "ACERUB", "ACESAC", 
"ACESPI", "ARANUD", "ARITRI", "ATHANG", "BETALL", "CARARC", "CARINT", 
"CINLAT", "CLIBOR", "DENPUN", "DRYCAM", "DRYINT", "FAGGRA", "FRAAME", 
"HUPLUC", "LONCAN", "MAICAN", "MAIRAC", "MEDVIR", "NABSPP", "OCLACU", 
"OXAMON", "PARNOV", "PHECON", "PICRUB", "RUBIDA", "SAMRAC", "STRAMP", 
"TIACOR", "TRIBOR", "TRIERE", "TRIUND", "TSUCAN", "UVUSES", "VIBLAN", 
"VIOBLA", "VIOROT"), all_3 = c(0.805, 0.84, 0.682, 0.764, 0.783, 
0.828, 0.799, 0.765, 0.704, 0.782, 0.771, 0.799, 0.733, 0.694, 
0.658, 0.82, 0.821, 0.766, 0.742, 0.721, 0.793, 0.782, 0.755, 
0.738, 0.62, 0.753, 0.711, 0.736, 0.657, 0.694, 0.769, 0.664, 
0.77, 0.735, 0.83, 0.709, 0.785, 0.749, 0.827, 0.744, 0.799), 
    topo_spectral_3 = c(0.729, 0.729, 0.592, 0.762, 0.668, 0.735, 
    0.736, 0.758, 0.626, 0.782, 0.589, 0.649, 0.753, 0.647, 0.633, 
    0.833, 0.821, 0.717, 0.667, 0.591, 0.76, 0.747, 0.699, 0.652, 
    0.522, 0.531, 0.696, 0.708, 0.704, 0.675, 0.739, 0.673, 0.678, 
    0.697, 0.705, 0.683, 0.717, 0.748, 0.725, 0.676, 0.793), 
    default = c(0.701, 0.553, 0.628, 0.605, 0.565, 0, 0.601, 
    0.574, 0, 0.5, 0, 0.566, 0, 0.544, 0.58, 0.582, 0.614, 0.5, 
    0.649, 0, 0, 0.465, 0.585, 0.528, 0, 0.619, 0.486, 0.536, 
    0.49, 0.586, 0.704, 0, 0.564, 0, 0.528, 0.556, 0, 0.497, 
    0, 0.533, 0.586)), row.names = c(NA, -41L), class = "data.frame")

I am preparing it for publication and want the whole table to be viewable without scrolling. The most apparent solution to me is to double the columns and hence cut the length in half.

I see this type of thing discussed in the LaTex community (https://tex.stackexchange.com/questions/134381/dealing-with-very-long-table-split-into-columns) but have not seen documentation for how to do this using R.

Image from the above link;

enter image description here

How can I make publication quality tables with double width as seen in the above image using R?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
nateroe
  • 487
  • 3
  • 20
  • 1
    Maybe check this https://stackoverflow.com/questions/55208340/wrapping-wide-table-in-rmarkdown – Duck Oct 05 '20 at 17:07
  • @Duck, thanks for the recommendation. They appear to be going from wide to long. I am attempting to do the opposite - from long to wide. Definitely related and I will see if I can reverse engineer their solution. It seems like there should be an R package that does this, no? It is not a terribly uncommon way to publish tables. Thanks! – nateroe Oct 05 '20 at 17:28
  • Look at `knitr` it has options for tables. – Duck Oct 05 '20 at 17:30

1 Answers1

2

That's easy using xtable:

xtable::xtable(long_table)

This code prints you LaTeX code to the console which you may copy into an Rmarkdown file. Don't put it into a code junk, put it just as normal text. Then just apply the instructions you shared in your question and insert some lines with the points to make the panels of equal length.

---
output: pdf_document
classoption:
  - a4paper
  - landscape
---
<!-- suppress page numbers  -->
\pagenumbering{gobble} 

\begin{table}[ht]
\centering
\begin{tabular}{rlrrr}
  \hline
 & species & all\_3 & topo\_spectral\_3 & default \\ 
  \hline
1 & ABIBAL & 0.80 & 0.73 & 0.70 \\ 
  2 & ACEPEN & 0.84 & 0.73 & 0.55 \\ 
  3 & ACERUB & 0.68 & 0.59 & 0.63 \\ 
  4 & ACESAC & 0.76 & 0.76 & 0.60 \\ 
  5 & ACESPI & 0.78 & 0.67 & 0.56 \\ 
  6 & ARANUD & 0.83 & 0.73 & 0.00 \\ 
  7 & ARITRI & 0.80 & 0.74 & 0.60 \\ 
  8 & ATHANG & 0.76 & 0.76 & 0.57 \\ 
  9 & BETALL & 0.70 & 0.63 & 0.00 \\ 
  10 & CARARC & 0.78 & 0.78 & 0.50 \\ 
  11 & CARINT & 0.77 & 0.59 & 0.00 \\ 
  12 & CINLAT & 0.80 & 0.65 & 0.57 \\ 
  13 & CLIBOR & 0.73 & 0.75 & 0.00 \\ 
  14 & DENPUN & 0.69 & 0.65 & 0.54 \\ 
  15 & DRYCAM & 0.66 & 0.63 & 0.58 \\ 
  16 & DRYINT & 0.82 & 0.83 & 0.58 \\ 
  17 & FAGGRA & 0.82 & 0.82 & 0.61 \\ 
  18 & FRAAME & 0.77 & 0.72 & 0.50 \\ 
  19 & HUPLUC & 0.74 & 0.67 & 0.65 \\ 
  20 & LONCAN & 0.72 & 0.59 & 0.00 \\ 
  . & . & . & . & . \\ 
  . & . & . & . & . \\ 
  . & . & . & . & . \\ 
  \hline
\end{tabular}
\begin{tabular}{rlrrr}
  \hline
 & species & all\_3 & topo\_spectral\_3 & default \\ 
  \hline
  . & . & . & . & . \\ 
  . & . & . & . & . \\ 
21 & MAICAN & 0.79 & 0.76 & 0.00 \\ 
  22 & MAIRAC & 0.78 & 0.75 & 0.46 \\ 
  23 & MEDVIR & 0.76 & 0.70 & 0.58 \\ 
  24 & NABSPP & 0.74 & 0.65 & 0.53 \\ 
  25 & OCLACU & 0.62 & 0.52 & 0.00 \\ 
  26 & OXAMON & 0.75 & 0.53 & 0.62 \\ 
  27 & PARNOV & 0.71 & 0.70 & 0.49 \\ 
  28 & PHECON & 0.74 & 0.71 & 0.54 \\ 
  29 & PICRUB & 0.66 & 0.70 & 0.49 \\ 
  30 & RUBIDA & 0.69 & 0.68 & 0.59 \\ 
  31 & SAMRAC & 0.77 & 0.74 & 0.70 \\ 
  32 & STRAMP & 0.66 & 0.67 & 0.00 \\ 
  33 & TIACOR & 0.77 & 0.68 & 0.56 \\ 
  34 & TRIBOR & 0.73 & 0.70 & 0.00 \\ 
  35 & TRIERE & 0.83 & 0.70 & 0.53 \\ 
  36 & TRIUND & 0.71 & 0.68 & 0.56 \\ 
  37 & TSUCAN & 0.78 & 0.72 & 0.00 \\ 
  38 & UVUSES & 0.75 & 0.75 & 0.50 \\ 
  39 & VIBLAN & 0.83 & 0.72 & 0.00 \\ 
  40 & VIOBLA & 0.74 & 0.68 & 0.53 \\ 
  41 & VIOROT & 0.80 & 0.79 & 0.59 \\ 
   \hline
\end{tabular}
\end{table}

enter image description here

You may want to look into the booktabs LaTeX package with which you can draw nicer lines using toprule, midrule, bottomrule.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    after pasting the code into an Rmarkdown (as plain text, not within a code block) I used 'Knit to HTML'. I opened the HTML file, it said; "% latex table generated in R 4.0.2 by xtable 1.8-4 package % Mon Oct 5 14:40:43 2020" but I did not see a table. How do I get the visual of the table like you provided. Sorry, I have never used LaTeX before. – nateroe Oct 05 '20 at 18:52
  • @nateroe Ah, I used knit to PDF. Just replace the entire example code that appears when opening a new .Rmd file in RStudio with the code above (without the `xtable` line), does that work? – jay.sf Oct 05 '20 at 19:24
  • 1
    Thank you - this worked! I had to 1. install LaTeX, tinytex (R package) or similar. 2. Within R, File > New File > Rmarkdown. Choose PDF as Default Output Format:. 3. Paste in code such as that provided by @jay.sf 4. 'Knit to PDF' – nateroe Oct 05 '20 at 19:54
  • Now that I have it in PDF format, if I am using it to prepare a publication or thesis, do you recommend taking a screen shot and pasting it into my word processor? – nateroe Oct 05 '20 at 19:55
  • @nateroe Thanks for sharing the relevant process on how you installed the additional software! Hm, I'm not using word anymore, wrote my entire thesis in rmarkdown, but my colleague does and I remember him wanting .pdf from me that he inserted into word, just like an image probably? – jay.sf Oct 05 '20 at 20:04
  • @nateroe Note: Just remembered, after the `\begin{table}[ht]` line you may insert a line `\footnotesize`, so the font becomes smaller during the table section and you may delete the `- landscape` line in the header (or set a `#` in front of it). – jay.sf Oct 05 '20 at 20:32