Hi all,
I would like to know if there is any fundamental difference (other than the row ordering) between using a combination of tidyr::expand()
and tidyr::nesting()
versus using dplyr::distinct()
. The following example can be found in the help file for tidyr::expand()
:
fruits <- tibble(
type = c("apple", "orange", "apple", "orange", "orange", "orange"),
year = c(2010, 2010, 2012, 2010, 2010, 2012),
size = factor(
c("XS", "S", "M", "S", "S", "M"),
levels = c("XS", "S", "M", "L")
),
weights = rnorm(6, as.numeric(size) + 2)
)
fruits %>% expand(nesting(type, size))
# A tibble: 4 x 2
type size
<chr> <fct>
1 apple XS
2 apple M
3 orange S
4 orange M
fruits %>% distinct(type, size)
# A tibble: 4 x 2
type size
<chr> <fct>
1 apple XS
2 orange S
3 apple M
4 orange M