From d@v|@ @end|ng |rom po@|t@co  Mon Oct  2 21:01:41 2023
From: d@v|@ @end|ng |rom po@|t@co (Davis Vaughan)
Date: Mon, 2 Oct 2023 15:01:41 -0400
Subject: [Rd] Should `expand.grid()` consistently drop `NULL` inputs?
Message-ID: <CABzLhzz11_7LyOUe3vzMN+rmkb7PS9cn4Yd7_LLdBihEK2QJsw@mail.gmail.com>

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



[Rd] Should `expand.grid()` consistently drop `NULL` inputs?
Davis Vaughan