Merck / metalite

An R package to create metadata structure for ADaM data analysis and reporting
https://merck.github.io/metalite/
GNU General Public License v3.0
15 stars 5 forks source link

keep original factor level for character in collect_n_subject() #168

Closed wangben718 closed 1 month ago

wangben718 commented 1 month ago

The factor level is overwritten unexpectedly by this line of code: https://github.com/Merck/metalite/blob/8d918310b89d10093694cb66c1ac4ef49674413a/R/collect_n_subject.R#L276

fb-elong commented 1 month ago

This is a recent change to only convert character values to factor in #165 . Could you give an example that create issues?

wangben718 commented 1 month ago
library(r2rtf)

meta <- metalite::meta_example()

meta$data_population$TRTA <- factor(meta$data_population$TRTA)
# Level SEX
meta$data_population$SEX <- factor(meta$data_population$SEX, levels = c("F", "M", "Unknown"))

meta <- meta |>
define_parameter(name = "sex", var = "SEX", label = "Sex")

collect_n_subject(meta, "apat", "sex")

Expect output:

$table
                name     Placebo Xanomeline Low Dose Xanomeline High Dose        Total
1 Number of Subjects          86                  84                   84          254
2                Sex        <NA>                <NA>                 <NA>         <NA>
3 Subjects with Data          86                  84                   84          254
4                  F 53 ( 61.6%)         50 ( 59.5%)          40 ( 47.6%) 143 ( 56.3%)
5                  M 33 ( 38.4%)         34 ( 40.5%)          44 ( 52.4%) 111 ( 43.7%)
6     Unknown  0(0%)                       0(0%)                         0(0%)  0(0%) 

Since Line 276 refactor the SEX variable, output is not showing row for Unknown as expected.

$table
                name     Placebo Xanomeline Low Dose Xanomeline High Dose        Total
1 Number of Subjects          86                  84                   84          254
2                Sex        <NA>                <NA>                 <NA>         <NA>
3 Subjects with Data          86                  84                   84          254
4                  F 53 ( 61.6%)         50 ( 59.5%)          40 ( 47.6%) 143 ( 56.3%)
5                  M 33 ( 38.4%)         34 ( 40.5%)          44 ( 52.4%) 111 ( 43.7%)
wangben718 commented 1 month ago

duplicated