result <-basic_table() %>%split_cols_by("ARM") %>%split_cols_by("AVISIT") %>%split_rows_by("TBILI_CAT") %>%# below split helps us get the right denominator between ALT/AST but it can be hiddensplit_rows_by("ALTAST_ind", split_fun =trim_levels_to_map(map), child_labels ="hidden") %>%count_occurrences(vars ="ALTAST_CAT",.stats ="fraction",denom ="n",drop =FALSE ) %>%append_topleft("Liver Laboratory Test Criterion") %>%build_table(df = adhy_liver)# trim away rows with criteria not metcriteria_fun <-function(tr) { row_label <-obj_label(tr)ifelse(row_label =="Criteria not met", TRUE, FALSE)}result <- result %>%trim_rows(criteria = criteria_fun)result
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
result <-basic_table() %>%split_cols_by("ARM") %>%split_cols_by("AVISIT") %>%split_rows_by("TBILI_CAT",split_fun =remove_split_levels("Total Bilirubin > 2xULN and Alkaline Phosphatase <= 2xULN") ) %>%# below split helps us get the right denominator between ALT/AST but it can be hiddensplit_rows_by("ALTAST_ind",split_fun =trim_levels_to_map(map), child_labels ="hidden" ) %>%count_occurrences(vars ="ALTAST_CAT",.stats ="fraction",denom ="n",drop =FALSE ) %>%append_topleft("Liver Laboratory Test Criterion") %>%build_table(df = adhy_liver)# trim away rows with criteria not metcriteria_fun <-function(tr) { row_label <-obj_label(tr)ifelse(row_label =="Criteria not met", TRUE, FALSE)}result <- result %>%trim_rows(criteria = criteria_fun)result
WebR is a tool allowing you to run R code in the web browser. Modify the code below and click run to see the results. Alternatively, copy the code and click here to open WebR in a new tab.
Code
library(dplyr)library(tern)adhy <- random.cdisc.data::cadhy# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.adhy_liver <-df_explicit_na(adhy)# Define values of interest in PARAMCD variable.paramcd_tbili_alt <-c("BLAL", "BGAL", "BA2AL", "BA5AL")paramcd_tbili_ast <-c("BLAS", "BGAS", "BA2AS", "BA5AS")# Select LBT09 parameters.adhy_liver <- adhy_liver %>%filter( SAFFL =="Y", AVISIT %in%c("BASELINE", "POST-BASELINE"), PARAMCD %in%c(paramcd_tbili_alt, paramcd_tbili_ast) )# Let's be explicit about factor levels for AVISIT and PARAMCD.adhy_liver <- adhy_liver %>%mutate(AVISIT =factor(AVISIT, levels =c("BASELINE", "POST-BASELINE")),PARAMCD =factor(PARAMCD, levels =c(paramcd_tbili_alt, paramcd_tbili_ast)) )# Create indicator and category variables.adhy_liver <- adhy_liver %>%mutate(# Create TBILI_CAT categories variable - this is needed so we get the right nesting in the table.TBILI_CAT =factor(case_when( PARAMCD %in%c(paramcd_tbili_alt[1], paramcd_tbili_ast[1]) ~"Total Bilirubin <= 2xULN", PARAMCD %in%c(paramcd_tbili_alt[2], paramcd_tbili_ast[2]) ~"Total Bilirubin > 2xULN", PARAMCD %in%c(paramcd_tbili_alt[3], paramcd_tbili_ast[3]) ~"Total Bilirubin > 2xULN and Alkaline Phosphatase <= 2xULN", PARAMCD %in%c(paramcd_tbili_alt[4], paramcd_tbili_ast[4]) ~"Total Bilirubin > 2xULN and Alkaline Phosphatase <= 5xULN" ),levels =c("Total Bilirubin <= 2xULN","Total Bilirubin > 2xULN","Total Bilirubin > 2xULN and Alkaline Phosphatase <= 2xULN","Total Bilirubin > 2xULN and Alkaline Phosphatase <= 5xULN" ) ),# Create ALTAST_CAT categories variable# this will be the labels for different ALT/AST categories displayed in the table.ALTAST_CAT =factor(case_when( PARAMCD %in% paramcd_tbili_alt & AVALC ==">3-5ULN"~"ALT >3 - <= 5xULN", PARAMCD %in% paramcd_tbili_alt & AVALC ==">5-10ULN"~"ALT >5 - <= 10xULN", PARAMCD %in% paramcd_tbili_alt & AVALC ==">10-20ULN"~"ALT >10 - <= 20xULN", PARAMCD %in% paramcd_tbili_alt & AVALC ==">20ULN"~"ALT > 20xULN", PARAMCD %in% paramcd_tbili_ast & AVALC ==">3-5ULN"~"AST >3 - <= 5xULN", PARAMCD %in% paramcd_tbili_ast & AVALC ==">5-10ULN"~"AST >5 - <= 10xULN", PARAMCD %in% paramcd_tbili_ast & AVALC ==">10-20ULN"~"AST >10 - <= 20xULN", PARAMCD %in% paramcd_tbili_ast & AVALC ==">20ULN"~"AST > 20xULN",TRUE~"Criteria not met" ),levels =c("ALT >3 - <= 5xULN", "ALT >5 - <= 10xULN", "ALT >10 - <= 20xULN","20"="ALT > 20xULN","AST >3 - <= 5xULN", "AST >5 - <= 10xULN", "AST >10 - <= 20xULN", "AST > 20xULN","Criteria not met" ) ),ALTAST_ind =factor(case_when( PARAMCD %in% paramcd_tbili_alt ~"ALT", PARAMCD %in% paramcd_tbili_ast ~"AST" ),levels =c("ALT", "AST") ) )map <-data.frame(ALTAST_ind =c(rep("ALT", 5), rep("AST", 5)),ALTAST_CAT =c("ALT >3 - <= 5xULN", "ALT >5 - <= 10xULN", "ALT >10 - <= 20xULN","20"="ALT > 20xULN", "Criteria not met","AST >3 - <= 5xULN", "AST >5 - <= 10xULN", "AST >10 - <= 20xULN", "AST > 20xULN", "Criteria not met" ),stringsAsFactors =FALSE)