Davis Vaughan
2023-Oct-02 19:01 UTC
[Rd] Should `expand.grid()` consistently drop `NULL` inputs?
Hi all, I noticed that `expand.grid()` has somewhat inconsistent behavior with dropping `NULL` inputs. In particular, if there is a leading `NULL`, then it ends up as a column in the resulting data frame, which seems pretty undesirable. Also, notice in the last example that `Var3` is used as the column name on the `NULL`, which is wrong. I think the most consistent behavior would be to unconditionally drop `NULL`s anywhere they appear (i.e. treat an `expand.grid()` call with `NULL` inputs as semantically equivalent to the same call without `NULL`s). ``` dropattrs <- function(x) { attributes(x) <- list(names = names(x)) x } # `NULL` dropped dropattrs(expand.grid(NULL)) #> named list() # `NULL` dropped dropattrs(expand.grid(1, NULL)) #> $Var1 #> numeric(0) # Oh no! Leading `NULL` ends up in the data frame! dropattrs(expand.grid(NULL, 1)) #> $Var2 #> NULL #> #> [[2]] #> numeric(0) # Oh no! This one does too! dropattrs(expand.grid(1, NULL, 2)) #> $Var1 #> numeric(0) #> #> $Var3 #> NULL #> #> [[3]] #> numeric(0) ``` Thanks, Davis