-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexercise_A2.R
46 lines (32 loc) · 898 Bytes
/
exercise_A2.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
library(rtables)
df <- data.frame(
ARM = factor(c("A", "B", "A"), levels = c("A", "B", "C")),
AGE = c(21, 23, 14)
)
is.factor(df$ARM)
levels(df$ARM)
lyt <- basic_table() |>
split_cols_by("ARM") |>
analyze("AGE", mean, format = "xx.xx")
tbl <- build_table(lyt, df)
print(tbl)
# note rtables keeps all levels by default
# this is standard behavior, e.g.
tapply(df$AGE, df$ARM, mean)
# To drop the levels with no observations use
# split functions
lyt2 <- basic_table() |>
split_cols_by("ARM", split_fun = drop_split_levels) |>
analyze("AGE", mean, format = "xx.xx")
tbl <- build_table(lyt2, df)
print(tbl)
# reorder columns
library(forcats)
library(dplyr)
df <- df |>
mutate(ARM = fct_relevel(ARM, "B", "A"))
lyt3 <- basic_table() |>
split_cols_by("ARM", split_fun = drop_split_levels) |>
analyze("AGE", mean, format = "xx.xx")
tbl <- build_table(lyt2, df)
print(tbl)