Reputation: 189
I'm looking to change specific values in a kableExtra
table.
Here is an example. I've created a summary statistics table outputted using kableExtra
. The statistics are based on the iris dataset and I've added in a Year variables.
iris %>%
mutate(Year = runif(n = 150, min = 2005, max = 2020) %>%
round(digits = 0)) %>%
sumtable(out = "kable",
vars = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Year"),
summ = c("notNA(x)", "mean(x)", "sd(x)", "min(x)", "max(x)"),
summ.names = c("Count", "Mean", "St. Dev.", "Min", "Max")) %>%
kable_styling()
For the last row, the mean and standard deviation are inapplicable, whereas the count, minimum, and maximum have some use.
Thus, I want to change the values for the mean and standard deviation of the row for Year to be either NA or -. Is there any way to accomplish that?
Thanks in advance.
Upvotes: 3
Views: 607
Reputation: 9240
You could return a dataframe, transform the required entries to NA
and then style the kable, e.g.
library(dplyr)
library(kableExtra)
library(vtable)
iris %>%
mutate(Year = runif(n = 150, min = 2005, max = 2020) %>%
round(digits = 0)) %>%
sumtable(out = "return",
vars = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Year"),
summ = c("notNA(x)", "mean(x)", "sd(x)", "min(x)", "max(x)"),
summ.names = c("Count", "Mean", "St. Dev.", "Min", "Max")) %>%
mutate(Mean = ifelse(Variable == "Year", NA_real_, Mean),
`St. Dev.` = ifelse(Variable == "Year", NA_real_, `St. Dev.`)) %>%
kable(digits = 2, caption = "Summary") %>%
kable_styling()
Upvotes: 2