我有这些数据,我需要将其显示为表格。我正在使用 R Markdown,输出文件是 PDF。
structure(list(`Census Year` = c(2020, 2010, 2000, 1990, 1980,
1970, 1960, 1950, 1940, 1930, 1920, 1910), Wisconsin = c(5893718,
5686986, 5363675, 4891769, 4705767, 4417731, 3951777, 3434575,
3137587, 2939006, 2632067, 2333860), Midwest = c(68985454, 66927001,
64392776, 59668632, 58865670, 56571663, 51619139, 44460762, 40143332,
38594100, 34019792, 29888542), USA = c(331449281, 308745538,
281421906, 248709873, 226545805, 203211926, 179323175, 151325798,
132165129, 123202660, 106021568, 92228531), `In Midwest` = c(8.54,
8.5, 8.33, 8.2, 7.99, 7.81, 7.66, 7.72, 7.82, 7.62, 7.74, 7.81
), `In USA` = c(1.78, 1.84, 1.91, 1.97, 2.08, 2.17, 2.2, 2.27,
2.37, 2.39, 2.48, 2.53)), row.names = c(NA, -12L), spec = structure(list(
cols = list(`Census Year` = structure(list(), class = c("collector_double",
"collector")), Wisconsin = structure(list(), class = c("collector_number",
"collector")), Midwest = structure(list(), class = c("collector_number",
"collector")), USA = structure(list(), class = c("collector_number",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x00000176450db8a0>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data. Frame"))
现在我想做一些事情。我已阅读文档,但仍有一些不清楚的地方。例如我需要:
kable() %>%
kable_classic() %>%
add_header_above(c(" " = 1, "Total Population" = 3, "Percentage Share" = 2),
align = list(c("c"), c("c", "c", "c", "c", "c")))
它并没有真正做太多事情,虽然最上面的顺序标题是居中对齐的,但第二个顺序和数据仍然是右对齐的。
2.我需要在表格的开头添加(可能是自动生成的)表格编号和表格标题(因为我有很多表格,因此是自动化的)
2.我需要提供来源,有时作为超链接。
3. 鉴于数据是人口,如何添加逗号分隔符(例如250,000)
\begin{table}[ht]
\caption{Census Population for Wisconsin, Midwest, and USA}
\label{tab:population}
\textbf{Source:} [Census Bureau](https://www.census.gov/data/tables/time-series/dec/popchange-data-text.html)
\end{table}
我对 Markdown 非常陌生,非常感谢您的帮助。
谢谢!
align=
的add_header_above
参数仅设置标题的对齐方式。要设置子标题和数据的对齐方式,您可以使用 align=
的 kable()
参数。
要将逗号作为大标记或分组标记,您可以使用
format.args=
的 kable()
参数。但是,由于这将应用于所有数字列,因此我将 Census Year
列转换为 character
。
library(kableExtra)
dat |>
as.data.frame() |>
within( `Census Year` <- as.character(`Census Year`)) |>
kable(
align = "c",
format.args = list(big.mark = ",")
) %>%
kable_classic() %>%
add_header_above(
header = c(" " = 1, "Total Population" = 3, "Percentage Share" = 2),
align = "c"
)