Zastanawiałem się, czy ktoś wie, jak dodać przypis pod Xtable, a także rozdzielić każdą kolumnę z pionową linią, aby ten stół wyglądał na nieco neater.

Chciałbym, żeby mój przypis powiedzieć "Są to wyniki testów ADF"

Powiedziałam mój kod i dataframe poniżej

{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)

Także, jak mam ogrodzić niektóre słowa w tabeli. Na przykład, gdybym chciał oszkolić "Korea Bond Flows", jak miałbym iść o to?

TIA!

0
Tanga94 15 grudzień 2019, 18:53

1 odpowiedź

Najlepsza odpowiedź

Nie używam xtable, ale masz kilka sugestii, które mogą być pomocne.

Po pierwsze, aby dodać przypis, można dołączyć:

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 = ""))

A potem, gdy tabela print dodaj add.to.row = comment, hline.after = c(-1, 0). Aby uzyskać więcej informacji na ten temat, zobacz Podobne Zapytanie.

Aby oddzielić kolumny z pionowymi liniami, użyj align i zapewnić wzór z wyrównaniem (na przykład w lewo lub prawej, na przykład) i gdzie chciałbyś podziały. Po wywołaniu funkcji xtable Dodaj opcję align = "ll|l|l|l|l|l" na przykład.

Do pogrubioną konkretne komórki w tabeli, dodaj kod dla tych elementów tabeli, takich jak pogrubienie dla Korea Bond Flows:

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

I użyj funkcji do wykrywania tego pogrubionego kodu:

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

I dodaj sanitize.text.function = bold.function do swojego print xtable.

Oto całe RMARKown, który wydaje się pracować. Uwaga, uciekłem z & w United States S&P 500 i dodano stringsAsFactors = FALSE podczas tworzenia ramy danych.

---
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)
```
0
Ben 16 grudzień 2019, 03:10