Home > Mobile >  Is there a base version of tidyr::expand?
Is there a base version of tidyr::expand?

Time:04-27

Is there an easy way or a built-in based function that is equivalent to tidyr::expand?

CodePudding user response:

To elaborate on the comment made by @onyambu, you could do

mtcars |> with(expand.grid(cyl=unique(cyl), am=unique(am)))
#   cyl am
# 1   6  1
# 2   4  1
# 3   8  1
# 4   6  0
# 5   4  0
# 6   8  0

whereas tidyr throws this:

library(magrittr)
mtcars %>% tidyr::expand(cyl, am)
# # A tibble: 6 × 2
#     cyl    am
#   <dbl> <dbl>
# 1     4     0
# 2     4     1
# 3     6     0
# 4     6     1
# 5     8     0
# 6     8     1
  • Related