Reducing Space Between Columns Without Changing Width in R Knitr Table
You want to reduce the space between columns without changing their width. Here’s an updated version of your code with full_width set to FALSE and the column widths adjusted:
library(knitr)
library(kableExtra)
# Create the table
tab <- rbind(
c("Grp1 & Grp2", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017"),
c("Grp1", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017"),
c("Grp1 & Grp2", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017"),
c("Grp1", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017"),
c("Grp1 & Grp2", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017"),
c("Grp1", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017"),
c("Grp1 & Grp2", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017"),
c("Grp1", "Jan 2015 - Dec 2017", "Jan 2016 - Dec 2016", "Jan 2017 - Dec 2017")
)
colnames(tab) <- c(' ','A1','A2','A1','A2','A1','A2','A1','A2','A1','A2','A1','A2')
rownames(tab) <- NULL
tab <- as.table(tab)
table_r_3_output <- kbl(tab, booktabs = T, linesep = "", row.names = 0, caption = "Results.") %>%
kable_styling(font_size = 5, latex_options = "hold_position", full_width = FALSE) %>%
column_spec(1, width = "19em") %>%
column_spec(c(2:13), width = "4em")
table_r_3_output
The output is:
This reduced the space between columns without changing their widths.
Last modified on 2023-12-22