View source: R/step-subset-expand.R
expand.dtplyr_step | R Documentation |
This is a method for the tidyr expand()
generic. It is translated to
data.table::CJ()
.
## S3 method for class 'dtplyr_step'
expand(data, ..., .name_repair = "check_unique")
data |
A |
... |
Specification of columns to expand. Columns can be atomic vectors or lists.
Unlike the data.frame method, this method does not use the full set of levels, just those that appear in the data. When used with continuous variables, you may need to fill in values
that do not appear in the data: to do so use expressions like
|
.name_repair |
Treatment of problematic column names:
This argument is passed on as |
library(tidyr)
fruits <- lazy_dt(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)
))
# All possible combinations ---------------------------------------
# Note that only present levels of the factor variable `size` are retained.
fruits %>% expand(type)
fruits %>% expand(type, size)
# This is different from the data frame behaviour:
fruits %>% dplyr::collect() %>% expand(type, size)
# Other uses -------------------------------------------------------
fruits %>% expand(type, size, 2010:2012)
# Use `anti_join()` to determine which observations are missing
all <- fruits %>% expand(type, size, year)
all
all %>% dplyr::anti_join(fruits)
# Use with `right_join()` to fill in missing rows
fruits %>% dplyr::right_join(all)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.