第 1 章 表格

这是一个稍微复杂一点的经典表格样式,常出现在论文或期刊的数值模拟比较部分1

library(dplyr)
library(kableExtra)
db <- mtcars[, 1:7]
db2 <- cbind(rownames(db), db)
colnames(db2) <- c("Methods", rep(c("Bias", "RMSE"), 3), "")
if (knitr::is_latex_output()) {
  kable(db2,
    format = "latex", booktabs = TRUE, escape = T, row.names = F,
    longtable = T, caption = "第1种类型的统计表格样式",
    linesep = c("", "", "", "", "", "\\midrule")
  ) %>%
    kable_styling(
      latex_options = c("hold_position", "repeat_header"),
      full_width = F, position = "center", repeat_header_method = "replace",
      repeat_header_text = "续表1.1"
    ) %>%
    add_header_above(c(" ",
      "$\\\\sigma^2$" = 2, "$\\\\phi$" = 2,
      "$\\\\tau^2$" = 2, "$r=\\\\delta/\\\\phi$" = 1
    ), escape = F) %>%
    footnote(
      general_title = "注:", title_format = "italic", threeparttable = T,
      general = "* 星号表示的内容可以很长"
    )
} else {
  kable(db2,
    format = "html", booktabs = TRUE, escape = T, row.names = F,
    caption = "第1种类型的统计表格样式"
  ) %>%
    kable_styling(
      bootstrap_options = c("basic"),
      full_width = F, position = "center"
    ) %>%
    add_header_above(c("",
      "$\\sigma^2$" = 2, "$\\phi$" = 2,
      "$\\tau^2$" = 2, "$r=\\delta/\\phi$" = 1
    ), escape = F) %>%
    footnote(
      general_title = "注:", title_format = "italic", threeparttable = T,
      general = "* 星号表示的内容可以很长"
    )
}
表 1.1: 第1种类型的统计表格样式
\(\sigma^2\)
\(\phi\)
\(\tau^2\)
\(r=\delta/\phi\)
Methods Bias RMSE Bias RMSE Bias RMSE
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02
Valiant 18.1 6 225.0 105 2.76 3.460 20.22
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60
注:
* 星号表示的内容可以很长
  • striped 默认使用浅灰色,stripe_color 可以用来指定颜色 stripe_color="white",它只在 LaTeX 下工作,HTML 下更改颜色需要设置 CSS,可以不使用 striped 改变默认的白底设置
  • threeparttable = TRUE 处理超长的注解标记,
  • add_header_above 函数内的 escape = F 用来处理数学公式,
  • longtable = T 表格很长时需要分页,因此使用续表,
  • hold_position 使用了 [!h] 控制浮动
  • 对于数学符号前要四个反斜杠这一点,作者今后可能会改变,只需要两个反斜杠,与 HTML 格式表格保持一致 https://github.com/haozhu233/kableExtra/issues/120
  • 对某些数据用不同颜色高亮 Selecting and colouring single table cells with kableExtra in R markdown cell_spec https://stackoverflow.com/questions/50118394

其实 Pandoc’s Markdown 本身也支持不少表格样式2,少见的比如

没有编号的可以跨行的表格.
First row 12.0 Example of a row that spans multiple lines.
Second row 5.0 Here’s another one. Note the blank line between rows.

1.1 软件信息

devtools::session_info(.packages(T))
## ─ Session info ──────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.5.2 (2017-01-27)
##  os       Ubuntu 14.04.5 LTS          
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       UTC                         
##  date     2019-02-22                  
## 
## ─ Packages ──────────────────────────────────────────────────────────────
##  package      * version  date       lib source        
##  askpass        1.1      2019-01-13 [1] CRAN (R 3.5.2)
##  assertthat     0.2.0    2017-04-11 [1] CRAN (R 3.5.2)
##  backports      1.1.3    2018-12-14 [1] CRAN (R 3.5.2)
##  base64enc      0.1-3    2015-07-28 [1] CRAN (R 3.5.2)
##  BH             1.69.0-1 2019-01-07 [1] CRAN (R 3.5.2)
##  bookdown       0.9      2018-12-21 [1] CRAN (R 3.5.2)
##  boot           1.3-20   2017-08-06 [3] CRAN (R 3.5.2)
##  callr          3.1.1    2018-12-21 [1] CRAN (R 3.5.2)
##  class          7.3-14   2015-08-30 [3] CRAN (R 3.5.2)
##  cli            1.0.1    2018-09-25 [1] CRAN (R 3.5.2)
##  clipr          0.5.0    2019-01-11 [1] CRAN (R 3.5.2)
##  clisymbols     1.2.0    2017-05-21 [1] CRAN (R 3.5.2)
##  cluster        2.0.7-1  2018-04-13 [3] CRAN (R 3.5.2)
##  codetools      0.2-15   2016-10-05 [3] CRAN (R 3.5.2)
##  colorspace     1.4-0    2019-01-13 [1] CRAN (R 3.5.2)
##  crayon         1.3.4    2017-09-16 [1] CRAN (R 3.5.2)
##  curl           3.3      2019-01-10 [1] CRAN (R 3.5.2)
##  desc           1.2.0    2018-05-01 [1] CRAN (R 3.5.2)
##  devtools       2.0.1    2018-10-26 [1] CRAN (R 3.5.2)
##  digest         0.6.18   2018-10-10 [1] CRAN (R 3.5.2)
##  dplyr        * 0.8.0.1  2019-02-15 [1] CRAN (R 3.5.2)
##  evaluate       0.13     2019-02-12 [1] CRAN (R 3.5.2)
##  fansi          0.4.0    2018-10-05 [1] CRAN (R 3.5.2)
##  foreign        0.8-71   2018-07-20 [3] CRAN (R 3.5.2)
##  fs             1.2.6    2018-08-23 [1] CRAN (R 3.5.2)
##  gh             1.0.1    2017-07-16 [1] CRAN (R 3.5.2)
##  git2r          0.24.0   2019-01-07 [1] CRAN (R 3.5.2)
##  glue           1.3.0    2018-07-17 [1] CRAN (R 3.5.2)
##  highr          0.7      2018-06-09 [1] CRAN (R 3.5.2)
##  hms            0.4.2    2018-03-10 [1] CRAN (R 3.5.2)
##  htmltools      0.3.6    2017-04-28 [1] CRAN (R 3.5.2)
##  httr           1.4.0    2018-12-11 [1] CRAN (R 3.5.2)
##  ini            0.3.1    2018-05-20 [1] CRAN (R 3.5.2)
##  jsonlite       1.6      2018-12-07 [1] CRAN (R 3.5.2)
##  kableExtra   * 1.0.1    2019-01-22 [1] CRAN (R 3.5.2)
##  KernSmooth     2.23-15  2015-06-29 [3] CRAN (R 3.5.2)
##  knitr          1.21     2018-12-10 [1] CRAN (R 3.5.2)
##  labeling       0.3      2014-08-23 [1] CRAN (R 3.5.2)
##  lattice        0.20-38  2018-11-04 [3] CRAN (R 3.5.2)
##  magrittr       1.5      2014-11-22 [1] CRAN (R 3.5.2)
##  markdown       0.9      2018-12-07 [1] CRAN (R 3.5.2)
##  MASS           7.3-51.1 2018-11-01 [3] CRAN (R 3.5.2)
##  Matrix         1.2-15   2018-11-01 [3] CRAN (R 3.5.2)
##  memoise        1.1.0    2017-04-21 [1] CRAN (R 3.5.2)
##  mgcv           1.8-26   2018-11-21 [3] CRAN (R 3.5.2)
##  mime           0.6      2018-10-05 [1] CRAN (R 3.5.2)
##  munsell        0.5.0    2018-06-12 [1] CRAN (R 3.5.2)
##  nlme           3.1-137  2018-04-07 [3] CRAN (R 3.5.2)
##  nnet           7.3-12   2016-02-02 [3] CRAN (R 3.5.2)
##  openssl        1.2.1    2019-01-17 [1] CRAN (R 3.5.2)
##  pillar         1.3.1    2018-12-15 [1] CRAN (R 3.5.2)
##  pkgbuild       1.0.2    2018-10-16 [1] CRAN (R 3.5.2)
##  pkgconfig      2.0.2    2018-08-16 [1] CRAN (R 3.5.2)
##  pkgload        1.0.2    2018-10-29 [1] CRAN (R 3.5.2)
##  plogr          0.2.0    2018-03-25 [1] CRAN (R 3.5.2)
##  prettyunits    1.0.2    2015-07-13 [1] CRAN (R 3.5.2)
##  processx       3.2.1    2018-12-05 [1] CRAN (R 3.5.2)
##  ps             1.3.0    2018-12-21 [1] CRAN (R 3.5.2)
##  purrr          0.3.0    2019-01-27 [1] CRAN (R 3.5.2)
##  R6             2.4.0    2019-02-14 [1] CRAN (R 3.5.2)
##  rcmdcheck      1.3.2    2018-11-10 [1] CRAN (R 3.5.2)
##  RColorBrewer   1.1-2    2014-12-07 [1] CRAN (R 3.5.2)
##  Rcpp           1.0.0    2018-11-07 [1] CRAN (R 3.5.2)
##  readr          1.3.1    2018-12-21 [1] CRAN (R 3.5.2)
##  remotes        2.0.2    2018-10-30 [1] CRAN (R 3.5.2)
##  rlang          0.3.1    2019-01-08 [1] CRAN (R 3.5.2)
##  rmarkdown      1.11     2018-12-08 [1] CRAN (R 3.5.2)
##  rpart          4.1-13   2018-02-23 [3] CRAN (R 3.5.2)
##  rprojroot      1.3-2    2018-01-03 [1] CRAN (R 3.5.2)
##  rstudioapi     0.9.0    2019-01-09 [1] CRAN (R 3.5.2)
##  rvest          0.3.2    2016-06-17 [1] CRAN (R 3.5.2)
##  scales         1.0.0    2018-08-09 [1] CRAN (R 3.5.2)
##  selectr        0.4-1    2018-04-06 [1] CRAN (R 3.5.2)
##  sessioninfo    1.1.1    2018-11-05 [1] CRAN (R 3.5.2)
##  spatial        7.3-11   2015-08-30 [3] CRAN (R 3.5.2)
##  stringi        1.3.1    2019-02-13 [1] CRAN (R 3.5.2)
##  stringr        1.4.0    2019-02-10 [1] CRAN (R 3.5.2)
##  survival       2.43-3   2018-11-26 [3] CRAN (R 3.5.2)
##  sys            2.1      2018-11-13 [1] CRAN (R 3.5.2)
##  tibble         2.0.1    2019-01-12 [1] CRAN (R 3.5.2)
##  tidyselect     0.2.5    2018-10-11 [1] CRAN (R 3.5.2)
##  tinytex        0.10     2019-01-10 [1] CRAN (R 3.5.2)
##  usethis        1.4.0    2018-08-14 [1] CRAN (R 3.5.2)
##  utf8           1.1.4    2018-05-24 [1] CRAN (R 3.5.2)
##  viridisLite    0.3.0    2018-02-01 [1] CRAN (R 3.5.2)
##  webshot        0.5.1    2018-09-28 [1] CRAN (R 3.5.2)
##  whisker        0.3-2    2013-04-28 [1] CRAN (R 3.5.2)
##  withr          2.1.2    2018-03-15 [1] CRAN (R 3.5.2)
##  xfun           0.5      2019-02-20 [1] CRAN (R 3.5.2)
##  xml2           1.2.0    2018-01-24 [1] CRAN (R 3.5.2)
##  xopen          1.0.0    2018-09-17 [1] CRAN (R 3.5.2)
##  yaml           2.2.0    2018-07-25 [1] CRAN (R 3.5.2)
## 
## [1] /home/travis/R/Library
## [2] /usr/local/lib/R/site-library
## [3] /home/travis/R-bin/lib/R/library