-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary table_1.R
48 lines (36 loc) · 1.18 KB
/
summary table_1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#x is independent variable,
#y is a vector of dependent variable;
#names of variable need to be put in a string format
#default p is false, if test need to be applied set p = TRUE
#ylab is a vector for labelling dependent variable; default is NULL
tbl_s <- function(x,y,data, p = FALSE, ylab = NULL){
require(flextable)
require(gtsummary)
require(Hmisc)
if(length(ylab) >= 1){
label(data)<- as.list(ylab[match(names(data),names(ylab))])
}
else{data <- data}
table <- data |>
select(all_of(c(x,y))) |>
tbl_summary(by = all_of(x)) |>
bold_labels() |>
add_overall()
if (p == TRUE){
table <- table |>
add_p()
}
else
{table <- table
}
table <- table |>
as_flex_table() |>
theme_box()
print(table)
}
#creating vector for labelling dependent variable
lab_y <- c("Sepal.Width" = "Sepal width","Sepal.Length" = "Sepal length")
#example using iris data set with default labels
tbl_s(x = "Species", y = c("Sepal.Width","Sepal.Length"), data = iris)
#using modified labels with p = TRUE
tbl_s(x = "Species", y = c("Sepal.Width","Sepal.Length"), data = iris, p = TRUE, ylab = lab_y)