atorus-research / Tplyr

https://atorus-research.github.io/Tplyr/
Other
95 stars 16 forks source link

Error: The number of values of your second variable must be greater than the number of levels in your first variable #166

Closed sukiegao closed 7 months ago

sukiegao commented 8 months ago

Prerequisites

Error: The number of values of your second variable must be greater than the number of levels in your first variable (Tplyr version 1.1.0)

Description

In nested CM table , the number of values of CMDECOD is less than the number of levels in ATC2, which caused the error, but in clinical study, this situation is possible since an CMDECOD can be assigned to multiple values of ATC2 in WHODrug. Just wondering why there is such a limitation, do we have other ways to deal with this situation in Tplyr?

Thanks!

Steps to Reproduce (Bug Report Only)

test_adsl <- data.frame( SUBJID = c("1", "2", "3"), TRT101A = c("TRT1", "TRT2", "TRT1") )

test_adcm <- data.frame( SUBJID = c("1", "2", "3"), ATC2 = c("Antiemetics and antinauseants", "Psycholeptics", "Psycholeptics"), CMDECOD = c("Promethazine hydrochloride", "Promethazine hydrochloride", "Promethazine hydrochloride"), TRT101A = c("TRT1", "TRT2", "TRT1") )

t1 <- test_adcm %>% tplyr_table(TRT101A) %>%
set_pop_data(test_adsl) %>% set_pop_treat_var(TRT101A) %>% add_layer( group_count("Any Previous Medications") %>% set_distinct_by(SUBJID) %>% set_format_strings(f_str("xxx (xx.x)", distinct_n, distinct_pct)) ) %>% add_layer( group_count(vars(ATC2, CMDECOD)) %>% set_nest_count(TRUE) %>% set_indentation("--->") %>% set_distinct_by(SUBJID) %>% set_format_strings(f_str("xxx (xx.x)", distinct_n, distinct_pct)) ) %>% build()

mstackhouse commented 8 months ago

@sukiegao are you looking for the summed up counts for the levels of ATC2 on this table? Or do you just need the indication of the ATC2 values?

The underlying logic for requiring this check is likely related to #146.

sukiegao commented 8 months ago

Many thanks for your quick response. Yes, we are looking for the summed up counts for levels of ATC2(as below table), such table is common for clinical trials. cmrelated1

So according to #146, is this error that I encountered just set to make sure the function set_order_count_method("bycount") feasible? Thanks.

mstackhouse commented 8 months ago

Ok got it - #146 is a bug we're working on fixing, but I'm thinking by fixing it we should be able to loosen this restriction. So set_order_count_method() unfortunately won't fix the problem you're encountering

sukiegao commented 8 months ago

Thanks for your explanation! Maybe loosen this restriction can fix the problem we're encountering.

mstackhouse commented 7 months ago

@sukiegao this should be resolved in the devel branch if you want to test. Thanks for providing your use case!