DeclareDesign / randomizr

randomizr: Easy-to-Use Tools for Common Forms of Random Assignment and Sampling
https://declaredesign.org/r/randomizr
Other
37 stars 9 forks source link

Unhelpful error if condition names are duplicated #21

Closed anniejw6 closed 7 years ago

anniejw6 commented 7 years ago

Maybe most users aren't stupid enough to make this mistake, but if you repeat an element in the condition_names vector, then you just get lots of warnings without an actual description of the error.

It's not a big deal with small datasets because you eventually just see the outcome, but if you're randomizing a large dataset with lots of strata, then you just get lots and lots of the same warning.

Might be a good idea to add a stopifnot(length(unique(condition_names)) == length(condition_names)) or similar?

See example below

> block_var <- rep(c("A", "B","C"), times = c(50, 100, 200))
> Z <- block_ra(block_var = block_var, prob_each = c(1/3, 1/3, 1/3), condition_names = c('C', 'T1', 'T1'))
Warning messages:
1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
> table(block_var, Z)
         Z
block_var   C  T1  T1
        A  17  33   0
        B  34  66   0
        C  67 133   0
Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

In my case, I was randomizing a dataset with a few thousand strata, so my screen just overloaded with warning messages.

acoppock commented 7 years ago

That is an unhelpful error. I'll add a check to condition_names to ensure they are unique. Appreciated!

acoppock commented 7 years ago

implemented in 40ce25d259dd04559dc01e6c9862a4d839cb5826.