Search code examples
rxtable

Seperating columns with a vertical line and adding a footnote in an Xtable (RMarkdown)


I was wondering if anyone knows how to add a footnote beneath an Xtable, as well as seperating each column with a vertical line to make this table look a bit neater.

I would like my footnote to say "these are ADF test results"

I've reproduced my code and dataframe below

{r table_3, echo = FALSE, warning = FALSE, message = FALSE, results = 'asis'}
library(tidyverse)
library(tidyselect)
library(xtable)
library(readxl)
library(knitr)

# Create Dataframe


variables <- c("Argentina Bond Flows", "Argentina Equity Flows", "Chile Bond Flows", "Chile Equity Flows", "Mexico Bond Flows",
               "Mexico Equity Flows", "Indonesia Bond Flows", "Indonesia Equity Flows", "Korea Bond Flows", "Korea Equity Flows",
               "Philippines Bond Flows", "Philippines Equity Flows", "Thailand Bond Flows", "Thailand Equity Flows", 
               "South Africa Bond Flows", "South Africa Equity Flows","United States S&P 500", "United States M1")


adf <- c("-4.3557","-6.4865","-3.4893","-3.3485","-2.6294","-5.511","-2.9238","-6.0305","-9.7081","-2.0444","-4.7619","-5.6108","-4.6314","-4.6218","-4.3337","-3.7213","2.6471"," 4.0174")
pp <- c("-6.9685","-9.3864","-6.7449","-5.6533","-6.0265","-9.4065","-6.531","-7.5043","-21.0764","-3.5716","-8.3505","-6.5574 ","-10.3266","-7.445","-8.9639","-6.3464","-11.1298","4.0512")

variables2 <- c("Argentina M1", "Argentina Equity Index", "Chile M1", "Chile Equity Index", "Mexico M1",
                "Mexico Equity Index", "Indonesia M1", "Indonesia Equity Index", "Korea M1", "Korea Equity Index",
                "Philippines M1", "Philippines Equity Index", "Thailand M1", "Thailand Equity Index", 
                "South Africa M1", "South Africa Equity Index","", "")


adf2 <- c("-6.9593","6.1785","3.6245","-2.2697","3.2417","2.4172","2.4237","2.0484","3.8211","-6.938","-6.9593","-7.7315","-6.9593","6.1785","-8.0415 ","2.5385","","")
pp2 <- c("-13.7725","4.8728","-11.0177","-9.3776","-14.3688 ","-2.2334","-13.1095","-8.8171","-11.1806","-9.8397","-13.7725","-9.4015","-2.6801","-11.8756","-10.681"," -9.6241","","")


unit_table <- data.frame(variables, adf, pp,variables2, adf2,pp2)
colnames(unit_table) <- c("Country Variable", "ADF", "PP","Country Variable", "ADF", "PP")

# Create a table using XTable

table <- xtable(unit_table, caption = "Unit Root Test Results \\label{Table3}",
                # tabular.environment = "longtable",
                floating = TRUE,
                table.placement = 'H',
                include.rownames = FALSE,
                # scalebox = 0.3,
                comment = FALSE,
                caption.placement = 'top'
                 )
bold <- function(x) {paste('{\\textbf{',x,'}}', sep ='')}


print(table, include.rownames = FALSE, sanitize.colnames.function=bold, comment = FALSE)

Also, how do I go about boldifying certain words in the table. For example if I wanted to boldify "Korea Bond Flows", how would I go about doing that?

TIA!


Solution

  • I don't use xtable much but have a few suggestions that might be helpful.

    First, to add a footnote, you can include the following:

    comment <- list(pos = list(0))
    comment$pos[[1]] <- c(nrow(unit_table))
    comment$command <- c(paste("\\hline\n", 
                               "These are ADF test results.\n", 
                               sep = ""))
    

    And then when you print the table, add add.to.row = comment, hline.after = c(-1, 0). For more details on this, see similar question.

    To separate columns with vertical lines, use align and provide pattern with alignment (left or right, for example) and where you would like divisions. When you call the xtable function add option align = "ll|l|l|l|l|l" for example.

    To bold specific cells in the table, add a code for those table elements, such as BOLD for Korea Bond Flows:

    unit_table[9, "Country Variable"] <- paste0("BOLD", unit_table[9, "Country Variable"])
    

    And use a function to detect this BOLD code:

    bold.function <- function(x) gsub('BOLD(.*)', paste('\\\\textbf{\\1','}'),x)
    

    And add sanitize.text.function = bold.function to your print of the xtable.

    Here is the entire RMarkdown that seems to work. Of note, I escaped your & in United States S&P 500 and added stringsAsFactors = FALSE when creating the data frame.

    ---
    title: "Test"
    output: pdf_document
    classoption: landscape
    ---
    
    ```{r, results="asis", echo = FALSE}
    
    library(xtable)
    
    variables <- c("Argentina Bond Flows", "Argentina Equity Flows", "Chile Bond Flows", "Chile Equity Flows", "Mexico Bond Flows",
                   "Mexico Equity Flows", "Indonesia Bond Flows", "Indonesia Equity Flows", "Korea Bond Flows", "Korea Equity Flows",
                   "Philippines Bond Flows", "Philippines Equity Flows", "Thailand Bond Flows", "Thailand Equity Flows", 
                   "South Africa Bond Flows", "South Africa Equity Flows","United States S\\&P 500", "United States M1")
    
    adf <- c("-4.3557","-6.4865","-3.4893","-3.3485","-2.6294","-5.511","-2.9238","-6.0305","-9.7081","-2.0444","-4.7619","-5.6108","-4.6314","-4.6218","-4.3337","-3.7213","2.6471"," 4.0174")
    pp <- c("-6.9685","-9.3864","-6.7449","-5.6533","-6.0265","-9.4065","-6.531","-7.5043","-21.0764","-3.5716","-8.3505","-6.5574 ","-10.3266","-7.445","-8.9639","-6.3464","-11.1298","4.0512")
    
    variables2 <- c("Argentina M1", "Argentina Equity Index", "Chile M1", "Chile Equity Index", "Mexico M1",
                    "Mexico Equity Index", "Indonesia M1", "Indonesia Equity Index", "Korea M1", "Korea Equity Index",
                    "Philippines M1", "Philippines Equity Index", "Thailand M1", "Thailand Equity Index", 
                    "South Africa M1", "South Africa Equity Index","", "")
    
    adf2 <- c("-6.9593","6.1785","3.6245","-2.2697","3.2417","2.4172","2.4237","2.0484","3.8211","-6.938","-6.9593","-7.7315","-6.9593","6.1785","-8.0415 ","2.5385","","")
    pp2 <- c("-13.7725","4.8728","-11.0177","-9.3776","-14.3688 ","-2.2334","-13.1095","-8.8171","-11.1806","-9.8397","-13.7725","-9.4015","-2.6801","-11.8756","-10.681"," -9.6241","","")
    
    unit_table <- data.frame(variables, adf, pp,variables2, adf2,pp2, stringsAsFactors = FALSE)
    colnames(unit_table) <- c("Country Variable", "ADF", "PP","Country Variable", "ADF", "PP")
    
    # Create a table using XTable
    
    unit_table[9, "Country Variable"] <- paste0("BOLD", unit_table[9, "Country Variable"])
    
    table <- xtable(unit_table, 
                    caption = "Unit Root Test Results \\label{Table3}",
                    # tabular.environment = "longtable",
                    floating = TRUE,
                    table.placement = 'H',
                    include.rownames = FALSE,
                    # scalebox = 0.3,
                    comment = FALSE,
                    caption.placement = 'top',
                    align = "ll|l|l|l|l|l"
    )
    
    # Additional code added
    
    bold.function <- function(x) gsub('BOLD(.*)', paste('\\\\textbf{\\1','}'),x)
    
    comment <- list(pos = list(0))
    comment$pos[[1]] <- c(nrow(unit_table))
    comment$command <- c(paste("\\hline\n", 
                               "These are ADF test results.\n", 
                               sep = ""))
    
    print(table, add.to.row = comment, hline.after = c(-1, 0), sanitize.text.function = bold.function)
    ```