3

I am trying to combine two tables in R Markdown into a single table, one below the other & retaining the header. The figure below shows the desired output. After putting my markdown code I will show the actual output. I realize that the way I have structured the pander statements will not allow me to get the output I want but searching SO I was unsuccessful in finding the right way to do so.

I can do some post processing in Word to get the output exactly as I want but I am trying to avoid that overhead.

The testdat.RData file is here: https://drive.google.com/file/d/0B0hTmthiX5dpWDd5UTdlbWhocVE/view?usp=sharing

The R Markdown RMD file is here: https://drive.google.com/file/d/0B0hTmthiX5dpSEFIcGRNQ1MzM1E/view?usp=sharing

Desired Output

Desired Output

```{r,echo=FALSE,message = FALSE, tidy=TRUE}
library(pander)
load("testdat.RData")
pander::pander(t1,big.mark=',', justify=c('left','right','right','right'))
pander::pander(t2,big.mark=',', justify=c('left','right','right','right'))
```

Actual Output

Actual Output

Thanks,

Krishnan

Krishnan
  • 1,265
  • 2
  • 13
  • 24
  • you need to physically combine them and add collumn names as additional rows – OganM Nov 10 '15 at 01:27
  • 1
    Manually doing this in markdown wouldn't be overly difficult, but obviously you don't want to (and shouldn't have to) do it manually. You might be able to do something like `pander(rbind(t1,names(t2),t2))` – Carl Nov 10 '15 at 01:28
  • 1
    Carl - This workaround helps. Thanks for the pointer. – Krishnan Nov 10 '15 at 01:37

2 Answers2

0

Here's my attempt using the xtable package:

```{r,echo=FALSE, message = FALSE, results="asis"}
library(xtable)

# Add thousands separator
t1[-1] = sapply(t1[-1], formatC, big.mark=",")
t2[-1] = sapply(t2[-1], formatC, big.mark=",")

t1$Mode = as.character(t1$Mode)

# Bind together t1, extra row of column names, and t2
t1t2 = rbind(t1, names(t1), t2)

# Render the table using xtable
print(xtable(t1t2, align="rrrrr"), # Right-align all columns (includes extra value for row names)
      include.rownames=FALSE, # Don't print rownames
      hline.after=NULL, 
      # Add midrules before/after each set column names
      add.to.row = list(pos = list(-1,0,4,5),
                        command = rep("\\midrule \n",4)))
```

And here's the output:

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thanks for your code. I ran it and here is the msg I get instead of the table. I think there might be pandoc/latex versioning issue. What version of latex (or Midtex - I am on a Windows 7 machine) are you using? *% latex table generated in R 3.1.3 by xtable 1.8-0 package %* Also, when i ran it on my real dataset I get this error: *Error in rbindlist(l, use.names, fill, idcol) : Item 2 of list input is not a data.frame, data.table or list Calls: ... eval -> eval -> rbind -> rbind -> -> rbindlist Execution halted* – Krishnan Nov 11 '15 at 14:34
  • I'm on a Mac running Yosemite. MacTEX 2015, R 3.2.2, xtable 1.7-4. – eipi10 Nov 13 '15 at 15:40
  • It was useful in my case but I would lose the `linestretch` specified in the YAML header, which instead was respected by `kable()` – Mr Frog Jul 18 '20 at 13:59
0

Allow me to make a formal answer since my comment seemed to work for you.

pander(rbind(t1,names(t2),t2))
Carl
  • 5,569
  • 6
  • 39
  • 74
  • What is the types of the first columns are different while the rest of the columns are the same type? – PM0087 Apr 02 '20 at 18:49