if (require("tables")) {
set.seed(42)
genders <- c("Male", "Female")
status <- c("low", "medium", "high")
Sex <- factor(sample(genders, 100, rep = TRUE))
Status <- factor(sample(status, 100, rep = TRUE))
z <- rnorm(100) + 5
fmt <- function(x) {
s <- format(x, digits = 2)
even <- ((1:length(s)) %% 2) == 0
s[even] <- sprintf("(%s)", s[even])
s
}
tab <- tabular(
Justify(c) * Heading() * z *
Sex * Heading(Statistic) *
Format(fmt()) *
(mean + sd) ~ Status
)
as_flextable(tab)
}
if (require("tables")) {
tab <- tabular(
(Species + 1) ~ (n = 1) + Format(digits = 2) *
(Sepal.Length + Sepal.Width) * (mean + sd),
data = iris
)
as_flextable(tab)
}
if (require("tables")) {
x <- tabular((Factor(gear, "Gears") + 1)
* ((n = 1) + Percent()
+ (RowPct = Percent("row"))
+ (ColPct = Percent("col")))
~ (Factor(carb, "Carburetors") + 1)
* Format(digits = 1), data = mtcars)
ft <- as_flextable(
x,
spread_first_col = TRUE,
row_title = as_paragraph(
colorize("Gears: ", color = "#666666"),
colorize(as_b(.row_title), color = "red")
)
)
ft
}
if (require("tables")) {
tab <- tabular(
(mean + mean) * (Sepal.Length + Sepal.Width) ~ 1,
data = iris
)
as_flextable(tab)
}
Run the code above in your browser using DataLab