-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path20_exercise.R
81 lines (59 loc) · 2.45 KB
/
20_exercise.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Create a basic adverse events table ----
library(rtables)
library(dplyr)
head(ex_adae[, c("USUBJID", "AEBODSYS", "AEDECOD")])
ex_adae2 <- ex_adae |>
mutate(
AEBODSYS = factor(sub("cl", "sys", AEBODSYS)),
AEDECOD = factor(sub("dcd", "code", AEDECOD))
)
s_events_patients <- function(x, labelstr, .N_col) {
in_rows(
"Total number of patients with at least one event" =
rcell(length(unique(x)) * c(1, 1 / .N_col), format = "xx (xx.xx%)"),
"Total number of events" = rcell(length(x), format = "xx")
)
}
s_events_patients(x = c("id 1", "id 1", "id 2"), .N_col = 5)
lyt <- basic_table() |>
split_cols_by("ARM", split_fun = add_overall_level("All Patients")) |>
add_colcounts() |>
summarize_row_groups("USUBJID", cfun = s_events_patients) |>
split_rows_by("AEBODSYS", child_labels = "visible", indent_mod = 1) |>
summarize_row_groups("USUBJID", cfun = s_events_patients) |>
analyze("AEDECOD", afun = \(x) as.list(table(x)), indent_mod = -1)
tbl <- build_table(lyt, ex_adae2, alt_counts_df = ex_adsl)
tbl
## remove (prune) rows with all zero counts
trim_zero_rows(tbl)
## Adverse Events by Grade Table ----
## sort highest count
table_count_grade_once_per_id <- function(df,
labelstr = "",
gradevar = "AETOXGR",
idvar = "USUBJID",
grade_levels = NULL) {
id <- df[[idvar]]
grade <- df[[gradevar]]
if (!is.null(grade_levels)) {
stopifnot(all(grade %in% grade_levels))
grade <- factor(grade, levels = grade_levels)
}
id_sel <- !duplicated(id)
in_rows(
"--Any Grade--" = sum(id_sel),
.list = as.list(table(grade[id_sel]))
)
}
lyt <- basic_table() |>
split_cols_by("ARM") |>
add_colcounts() |>
analyze("USUBJID", afun = s_events_patients, show_labels = "hidden") |>
split_rows_by("AEBODSYS", split_fun = add_overall_level("- Any adverse events - "), child_labels = "visible") |>
summarize_row_groups(cfun = table_count_grade_once_per_id, format = "xx", indent_mod = 0) |>
split_rows_by("AEDECOD", split_fun = drop_split_levels, child_labels = "visible", indent_mod = -1, section_div = " ") |>
analyze("AETOXGR",
afun = table_count_grade_once_per_id,
extra_args = list(grade_levels = 1:5), show_labels = "hidden")
tbl <- build_table(lyt, ex_adae, alt_counts_df = ex_adsl)
tbl