library(dplyr)
df <- data.frame(
USUBJID = as.character(c(
1, 1, 2, 4, 4, 4,
6, 6, 6, 7, 7, 8
)),
MHDECOD = c(
"MH1", "MH2", "MH1", "MH1", "MH1", "MH3",
"MH2", "MH2", "MH3", "MH1", "MH2", "MH4"
),
ARM = rep(c("A", "B"), each = 6),
SEX = c("F", "F", "M", "M", "M", "M", "F", "F", "F", "M", "M", "F")
)
df_adsl <- df %>%
select(USUBJID, ARM) %>%
unique()
# Create table layout
lyt <- basic_table() %>%
split_cols_by("ARM") %>%
add_colcounts() %>%
count_occurrences(vars = "MHDECOD", .stats = c("count_fraction"))
# Apply table layout to data and produce `rtable` object
tbl <- lyt %>%
build_table(df, alt_counts_df = df_adsl) %>%
prune_table()
tbl
# Layout creating function with custom format.
basic_table() %>%
add_colcounts() %>%
split_rows_by("SEX", child_labels = "visible") %>%
summarize_occurrences(
var = "MHDECOD",
.formats = c("count_fraction" = "xx.xx (xx.xx%)")
) %>%
build_table(df, alt_counts_df = df_adsl)
# Count unique occurrences per subject.
s_count_occurrences(
df,
.N_col = 4L,
.N_row = 4L,
.df_row = df,
.var = "MHDECOD",
id = "USUBJID"
)
a_count_occurrences(
df,
.N_col = 4L,
.df_row = df,
.var = "MHDECOD",
id = "USUBJID"
)
Run the code above in your browser using DataLab