posterior icon indicating copy to clipboard operation
posterior copied to clipboard

subset_draws duplicates variables if `variables` argument contains `NA`

Open n-kall opened this issue 1 year ago • 4 comments

When subsetting by variable, if NA is included in the variable argument along with other strings, the matched variables are duplicated in the output.

Example:

example_draws() |>
subset_draws(variable = c("mu", NA)) |>
variables()

#> [1] "mu" "mu"

n-kall avatar May 08 '24 09:05 n-kall

Looks like an issue with the check_existing_variables() helper:

(check_existing_variables(c("mu", NA), example_draws()))
## [1] "mu" "mu"

mjskay avatar May 08 '24 19:05 mjskay

Great, probably not too complex of a fix then. But I also wonder if it ever makes sense to allow NA in variable. Maybe there should be an input check disallowing this?

n-kall avatar May 08 '24 19:05 n-kall

Yeah good point. The two options to my mind are:

  • throw an error in check_existing_variables() if any elements of variable are NA.
  • something consistent with how variable selection works in base R subsetting, like returning a variable that is ndraws(x) of NA for that entry in the output.

I think I'd be fine with throwing an error since it probably isn't that useful to do variable subsetting by names with NAs, and throwing an error would be simpler. Then if a use case arises for this later we could consider implementing it.

mjskay avatar May 08 '24 19:05 mjskay

@mjskay I think your first suggestion (throwing an error if names includes NA) would make the most sense at this stage

n-kall avatar Jun 04 '24 08:06 n-kall

I agree. @n-kall would you mind making a small PR to fix this issue?

paul-buerkner avatar Dec 17 '24 17:12 paul-buerkner

Sure, can do

n-kall avatar Dec 17 '24 20:12 n-kall