Table Display

kable

kable(x, format, digits = getOption(“digits”), row.names = NA, col.names = NA, align, caption = NULL, label = NULL, format.args = list(), escape = TRUE, …)


The function kable is a simple table generator, from library knitr

  • digits: number of digits for individual columns
  • align: ‘l’(left), ‘c’(center),‘r’(right) ex) c(‘c’,‘l’,‘c’)
  • caption
  • label
knitr::kable(mtcars, 
             digits = 2,
             caption = "table 1 caption")
table 1 caption
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.62 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.88 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.32 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.21 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.44 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.46 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.57 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.19 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.15 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.44 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.25 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.42 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.34 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.20 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.62 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.84 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.46 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.52 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.44 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.84 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.85 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.94 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.14 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.51 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.17 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.77 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.57 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.78 18.60 1 1 4 2

DT

datatable(data, options = list(), class = “display”, callback = JS(“return table;”), rownames, colnames, container, caption = NULL, filter = c(“none”, “bottom”, “top”), escape = TRUE, style = “default”, width = NULL, height = NULL, elementId = NULL, fillContainer = getOption(“DT.fillContainer”, NULL), autoHideNavigation = getOption(“DT.autoHideNavigation”, NULL), selection = c(“multiple”, “single”, “none”), extensions = list(), plugins = NULL, editable = FALSE)


  • caption
  • filter: add filter to each column
  • editable: TRUE/FALSE
  • colnames: colnames = c(‘Here’, ‘Are’, ‘Some’, ‘New’, ‘Names’) colnames = c(‘A Better Name’ = ‘Sepal.Width’) colnames = c(‘Another Better Name’ = 2, ‘Yet Another Name’ = 4
  • pageLength(options): limited showing row c(“none”, “bottom”, “top”)
  • scrollX(options): get vertical scroll bar
library(DT)
longley <- data.frame(longley)
datatable(longley, rownames = FALSE, filter="top", options = list(pageLength = 6, scrollX=T) )

tibble

tibble(…, .rows = NULL, .name_repair = c(“check_unique”, “unique”, “universal”, “minimal”))


  • Re-imagining of the data.frame
  • tibble() never changes the type of the inputs,never changes the names of variables, easier than data.frame()
library(tibble)
as_tibble(longley)
## # A tibble: 16 x 7
##    GNP.deflator   GNP Unemployed Armed.Forces Population  Year Employed
##           <dbl> <dbl>      <dbl>        <dbl>      <dbl> <int>    <dbl>
##  1         83    234.       236.         159        108.  1947     60.3
##  2         88.5  259.       232.         146.       109.  1948     61.1
##  3         88.2  258.       368.         162.       110.  1949     60.2
##  4         89.5  285.       335.         165        111.  1950     61.2
##  5         96.2  329.       210.         310.       112.  1951     63.2
##  6         98.1  347.       193.         359.       113.  1952     63.6
##  7         99    365.       187          355.       115.  1953     65.0
##  8        100    363.       358.         335        116.  1954     63.8
##  9        101.   397.       290.         305.       117.  1955     66.0
## 10        105.   419.       282.         286.       119.  1956     67.9
## 11        108.   443.       294.         280.       120.  1957     68.2
## 12        111.   445.       468.         264.       122.  1958     66.5
## 13        113.   483.       381.         255.       123.  1959     68.7
## 14        114.   503.       393.         251.       125.  1960     69.6
## 15        116.   518.       481.         257.       128.  1961     69.3
## 16        117.   555.       401.         283.       130.  1962     70.6
tibble(longley$GNP, longley$Unemployed, total = longley$GNP + longley$Unemployed)
## # A tibble: 16 x 3
##    `longley$GNP` `longley$Unemployed` total
##            <dbl>                <dbl> <dbl>
##  1          234.                 236.  470.
##  2          259.                 232.  492.
##  3          258.                 368.  626.
##  4          285.                 335.  620.
##  5          329.                 210.  539.
##  6          347.                 193.  540.
##  7          365.                 187   552.
##  8          363.                 358.  721.
##  9          397.                 290.  688.
## 10          419.                 282.  701.
## 11          443.                 294.  736.
## 12          445.                 468.  913.
## 13          483.                 381.  864.
## 14          503.                 393.  896.
## 15          518.                 481.  999.
## 16          555.                 401.  956.

paged

rmarkdown::paged_table(mtcars)

xtable

xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, auto = FALSE, …)

library(xtable)
f1 <- aov(Year ~ GNP + Unemployed +  Population, data = longley) #latex input
xtable(f1)
## % latex table generated in R 3.6.1 by xtable 1.8-4 package
## % Sat Aug 24 09:14:03 2019
## \begin{table}[ht]
## \centering
## \begin{tabular}{lrrrrr}
##   \hline
##  & Df & Sum Sq & Mean Sq & F value & Pr($>$F) \\ 
##   \hline
## GNP & 1 & 336.79 & 336.79 & 6298.74 & 0.0000 \\ 
##   Unemployed & 1 & 2.39 & 2.39 & 44.76 & 0.0000 \\ 
##   Population & 1 & 0.17 & 0.17 & 3.20 & 0.0987 \\ 
##   Residuals & 12 & 0.64 & 0.05 &  &  \\ 
##    \hline
## \end{tabular}
## \end{table}

stargazer

stargazer( …, type = “latex”, title = “”, style = “default”, summary = NULL, out = NULL, out.header = FALSE, column.labels = NULL, column.separate = NULL, covariate.labels = NULL, dep.var.caption = NULL, dep.var.labels = NULL, dep.var.labels.include = TRUE, align = FALSE, coef = NULL, se = NULL, t = NULL, p = NULL, t.auto = TRUE, p.auto = TRUE, ci = FALSE, ci.custom = NULL, ci.level = 0.95, ci.separator = NULL, add.lines = NULL, apply.coef = NULL, apply.se = NULL, summary.logical = TRUE, summary.stat = NULL, nobs = TRUE, mean.sd = TRUE, min.max = TRUE, median = FALSE, iqr = FALSE )


  • Type: “text”, “html”
  • ci: TRUE/FALSE
  • ci.level: 0.8, 0.9
  • omit.stat: =c(“f”, “ser”)
library(stargazer)
f1 <- lm(mpg ~ wt, mtcars) #2 OLS models
f2 <- lm(mpg ~ wt + hp, mtcars)
f3 <- lm(mpg ~ wt + hp + cyl, mtcars)
stargazer(f1, f2, f3, type = 'html')
Dependent variable:
mpg
(1) (2) (3)
wt -5.344*** -3.878*** -3.167***
(0.559) (0.633) (0.741)
hp -0.032*** -0.018
(0.009) (0.012)
cyl -0.942*
(0.551)
Constant 37.285*** 37.227*** 38.752***
(1.878) (1.599) (1.787)
Observations 32 32 32
R2 0.753 0.827 0.843
Adjusted R2 0.745 0.815 0.826
Residual Std. Error 3.046 (df = 30) 2.593 (df = 29) 2.512 (df = 28)
F Statistic 91.375*** (df = 1; 30) 69.211*** (df = 2; 29) 50.171*** (df = 3; 28)
Note: p<0.1; p<0.05; p<0.01
library(stargazer)
f1 <- lm(mpg ~ wt, mtcars) #2 OLS models
f2 <- lm(mpg ~ wt + hp, mtcars)
f3 <- lm(mpg ~ wt + hp + cyl, mtcars)
stargazer(f1, f2, f3, type = 'html')
Dependent variable:
mpg
(1) (2) (3)
wt -5.344*** -3.878*** -3.167***
(0.559) (0.633) (0.741)
hp -0.032*** -0.018
(0.009) (0.012)
cyl -0.942*
(0.551)
Constant 37.285*** 37.227*** 38.752***
(1.878) (1.599) (1.787)
Observations 32 32 32
R2 0.753 0.827 0.843
Adjusted R2 0.745 0.815 0.826
Residual Std. Error 3.046 (df = 30) 2.593 (df = 29) 2.512 (df = 28)
F Statistic 91.375*** (df = 1; 30) 69.211*** (df = 2; 29) 50.171*** (df = 3; 28)
Note: p<0.1; p<0.05; p<0.01
correlation.matrix <- cor(mtcars) #correlation matrix
stargazer(correlation.matrix, type="html")
mpg cyl disp hp drat wt qsec vs am gear carb
mpg 1 -0.852 -0.848 -0.776 0.681 -0.868 0.419 0.664 0.600 0.480 -0.551
cyl -0.852 1 0.902 0.832 -0.700 0.782 -0.591 -0.811 -0.523 -0.493 0.527
disp -0.848 0.902 1 0.791 -0.710 0.888 -0.434 -0.710 -0.591 -0.556 0.395
hp -0.776 0.832 0.791 1 -0.449 0.659 -0.708 -0.723 -0.243 -0.126 0.750
drat 0.681 -0.700 -0.710 -0.449 1 -0.712 0.091 0.440 0.713 0.700 -0.091
wt -0.868 0.782 0.888 0.659 -0.712 1 -0.175 -0.555 -0.692 -0.583 0.428
qsec 0.419 -0.591 -0.434 -0.708 0.091 -0.175 1 0.745 -0.230 -0.213 -0.656
vs 0.664 -0.811 -0.710 -0.723 0.440 -0.555 0.745 1 0.168 0.206 -0.570
am 0.600 -0.523 -0.591 -0.243 0.713 -0.692 -0.230 0.168 1 0.794 0.058
gear 0.480 -0.493 -0.556 -0.126 0.700 -0.583 -0.213 0.206 0.794 1 0.274
carb -0.551 0.527 0.395 0.750 -0.091 0.428 -0.656 -0.570 0.058 0.274 1