I've got a table I'm fairly happy with. I've added a row of column averages, and I'd like add the word "Averages" to the left of it. I've tried adding a column (labeled ""
) but to no avail. Here's what I've got:
rubric_results <- tibble(
"PLCO-1" = c(4, 5, 4, 4, 3.5, 3, 5, 4.5, 5),
"PLCO-2" = c(4, 5, 4, 5, 4, 3, 5, 5, 5),
"PLCO-3" = c(4, 5, 4, 4, 4, 4, 5, 4, 5),
"PLCO-4" = c(4.33, 5, 4.33, 3.33, 3.67, 2.67, 5, 4.33, 5),
"PLCO-5" = c(3.67, 5, 4, 3, 3.67, 3, 5, 4, 4.33)
)
averages <- colMeans(rubric_results, na.rm = TRUE)
rubric_results <- bind_rows(rubric_results,
tibble("PLCO-1" = averages[1], "PLCO-2" = averages[2],
"PLCO-3" = averages[3], "PLCO-4" = averages[4],
"PLCO-5" = averages[5])) %>%
mutate(across(everything(), ~round(., 2)))
headers = c("Summary of Findings" = 5)
rubric_results %>%
kable("latex", booktabs = TRUE, caption = "\\vspace{-2mm} \\Large Title") %>%
kable_styling(latex_options = c("hold_position")) %>%
add_header_above(headers) %>%
row_spec(nrow(rubric_results) - 1, hline_after = TRUE) %>%
to row_spec(nrow(rubric_results), bold = TRUE)
My sense is the solution will involve a new column, but I cannot make it work. Thanks in advance!
If you set the final rowname of the dataframe you give to kable()
to "Average", it will be displayed. One issue is that tibbles don't support rownames, so you could convert to a dataframe first, e.g. using
rubric_results %>%
as.data.frame() %>%
...
The other rownames default to 1:9; you could change them too if you want, but they aren't allowed to be duplicates, so they can't all be blank.
Another approach (the one tibbles prefer) is simply to add another column at the start of the tibble, e.g.
rubric_results <- cbind(" " = c(rep("", 9), "Average"), rubric_results)
This will leave blanks on the other rows other than the last one.