I have this data which I need to display as a table. I am using R Markdown and the output file is a 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"))
Now there are a few things I am looking to do. I have read the documentation, but few things are still unclear. For example I need:
- Center all headings, subheadings, and data. (I am having a bit of a difficulty in here.
kable() %>%
kable_classic() %>%
add_header_above(c(" " = 1, "Total Population" = 3, "Percentage Share" = 2),
align = list(c("c"), c("c", "c", "c", "c", "c")))
It is not really doing much, and while the top order headings are center aligne, the second order and the data are still right aligned.
2. I need to add (possibly auto-generated) table numbers and table title at the beginning of the table (as I have a good number of tables, hence the automation)
2. I need to provide source, sometimes as hyperlink.
3. Given the data is for population, how to add comma separator (for example 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}
I am very new to markdown and your help is greatly appreciated.
Thanks!
The
align=argument ofadd_header_aboveonly sets the alignment of the header. To set the alignment for the subheaders and the data you could use thealign=argument ofkable().To have a comma as the big or grouping mark you could use the
format.args=argument ofkable(). However, as this will applied to all numeric columns I convert theCensus Yearcolumn to acharacter.