11# Read in ADEX dataset
22load(" data/metalite_ae_adex.rda" )
3-
43adex <- metalite_ae_adex
54
5+ # Summarize exposure duration
66adex1 <- adex | >
77 dplyr :: group_by(USUBJID ) | >
88 dplyr :: mutate(
@@ -16,11 +16,112 @@ adex1 <- adex |>
1616 ) | >
1717 dplyr :: distinct(USUBJID , .keep_all = TRUE )
1818
19- # Assign Label to derived variables
20- Hmisc :: label(adex1 $ AVAL ) <- " Analysis Value"
21- Hmisc :: label(adex1 $ PARAM ) <- " Parameter"
22- Hmisc :: label(adex1 $ PARAMCD ) <- " Parameter Code"
19+ # using attr() to assign labels
20+ attr(adex1 $ AVAL , " label" ) <- " Analysis Value"
21+ attr(adex1 $ PARAM , " label" ) <- " Parameter"
22+ attr(adex1 $ PARAMCD , " label" ) <- " Parameter Code"
23+
24+ adex1 $ EXDURDDU <- structure(adex1 $ EXDURDDU , label = " New Label for EXDURDDU" )
25+
26+ # get rid of class labelled made with haven, Hmisc package
27+ class(adex1 $ EXDURDDU ) <- NULL
28+ class(adex1 $ EXNUMDOS ) <- NULL
29+
30+ attr(adex1 $ EXDURDDU , " label" ) <- " Exposure Duration Unit"
31+ attr(adex1 $ EXNUMDOS , " label" ) <- " Number of Daily Doses"
32+
33+
34+ # --------------------------------------------------
35+ # New code: Simulated treatment duration dataset
36+ # --------------------------------------------------
37+
38+ # The ADaM dataset for Drug Exposrue Summary Data, is utilized to:
39+ #
40+ # - Sum up duration by STUDYID SITENUM USUBJID SUBJID APERIOD EXTRT ADOSEFRM PARAMCD.
41+ # - Subset the exposure data by `upcase(trim(left(paramcd))) = "TRTDUR"`.
42+ # - Get the exposure duration `adexsum$AVAL` for all participants.
43+ # - Assign duration category `adexsum$EXDURGR` i.e.">=1 day", ">=7 days",">=28 days", ">=12 weeks" and ">=24 weeks".
44+
45+ set.seed(123 ) # For reproducibility, keeping the rest of the code unchanged
46+
47+ adexsum <- r2rtf :: r2rtf_adsl | >
48+ dplyr :: select(USUBJID , TRT01A , TRT01P , AGE , AGEU , AGEGR1 , SEX , RACE , RACEN , TRTSDT , SAFFL ) | >
49+ dplyr :: mutate(
50+ APERIODC = " Base" ,
51+ APERIOD = 1 ,
52+ PARQUAL = " All" ,
53+ PARAM = " Treatment Duration Actual in Days" ,
54+ PARAMCD = " TRTDURD" ,
55+ AVAL = sample(x = 0 : (24 * 7 ), size = dplyr :: n(), replace = TRUE ),
56+ EXDURGR = dplyr :: case_when(
57+ AVAL > = 24 * 7 ~ " >=24 weeks" ,
58+ AVAL > = 12 * 7 ~ " >=12 weeks" ,
59+ AVAL > = 28 ~ " >=28 days" ,
60+ AVAL > = 7 ~ " >=7 days" ,
61+ AVAL > = 1 ~ " >=1 day"
62+ )
63+ )
64+
65+ adexsum $ EXDURGR <- factor (adexsum $ EXDURGR ,
66+ levels = c(" not treated" , " >=1 day" , " >=7 days" , " >=28 days" , " >=12 weeks" , " >=24 weeks" )
67+ )
68+
69+ adexsum $ TRTA <- factor (adexsum $ TRT01A ,
70+ levels = c(" Placebo" , " Xanomeline Low Dose" , " Xanomeline High Dose" ),
71+ labels = c(" Placebo" , " Low Dose" , " High Dose" )
72+ )
73+
74+ # To combine both ADEX1 and ADEXSUM, need to handle the following:
75+ # Ungrouped grouped data frames
76+ # Normalized only numeric labelled columns
77+ # Preserved character-labelled columns as character
78+ # Added missing columns without overwriting existing ones (some vars in ADEX1 only and some in ADEXSUM only)
79+
80+ library(dplyr )
81+
82+ safe_bind_rows <- function (df1 , df2 ) {
83+ # Remove grouping
84+ df1 <- dplyr :: ungroup(df1 )
85+
86+ # Normalize labelled columns (strip class, keep label attribute)
87+ normalize_labelled <- function (df ) {
88+ df [] <- lapply(df , function (x ) {
89+ if (inherits(x , " labelled" )) {
90+ base <- unclass(x ) # remove haven_labelled class
91+ lbl <- attr(x , " label" , exact = TRUE )
92+
93+ # Preserve type and label
94+ if (is.numeric(base )) {
95+ base <- as.numeric(base )
96+ } else {
97+ base <- as.character(base )
98+ }
99+ if (! is.null(lbl )) attr(base , " label" ) <- lbl
100+ base
101+ } else {
102+ x
103+ }
104+ })
105+ df
106+ }
107+
108+ # Apply normalization
109+ df1 <- normalize_labelled(df1 )
110+ df2 <- normalize_labelled(df2 )
111+
112+ # Align columns without overwriting
113+ all_cols <- union(names(df1 ), names(df2 ))
114+ for (col in all_cols ) {
115+ if (! col %in% names(df1 )) df1 [[col ]] <- NA
116+ if (! col %in% names(df2 )) df2 [[col ]] <- NA
117+ }
118+
119+ # Bind rows safely
120+ dplyr :: bind_rows(df1 , df2 )
121+ }
23122
24- metalite_ae_adexsum <- adex1
123+ # Use the function
124+ metalite_ae_adexsum <- safe_bind_rows(adex1 , adexsum )
25125
126+ # Save the extended dataset
26127usethis :: use_data(metalite_ae_adexsum , overwrite = TRUE )
0 commit comments