Generates a new data frame (in the form of a tibble)
Details
By default, all specified variables vary across their range while all other variables are held constant at their reference value. Types, classes, factor levels and time zones are always preserved. The user can specify the length of each sequence, require that only observed values and combinations are used and add new variables.
Naming an argument generates a new column of that name
rather than varying an existing column.
A bare vector or a one column data frame becomes the new column,
so Annual = annual keeps annual at its reference value and adds
Annual varying across its range.
A data frame with more than one column, such as the result of
tidyr::nesting(), is packed into a data frame column of that name.
An argument that evaluates to NULL is dropped.
Examples
data <- tibble::tibble(
period = factor(c("before", "before", "after", "after"),
levels = c("before", "after")
),
count = c(0L, 1L, 5L, 4L),
annual = factor(c(2, 3, 5, 8), levels = c(1, 2, 3, 5, 8))
)
# By default all other variables are held constant at their reference value.
xnew_data(data)
#> # A tibble: 1 × 3
#> period count annual
#> <fct> <int> <fct>
#> 1 before 2 1
# Specifying a variable causes it to vary across its range.
xnew_data(data, annual)
#> # A tibble: 5 × 3
#> period count annual
#> <fct> <int> <fct>
#> 1 before 2 1
#> 2 before 2 2
#> 3 before 2 3
#> 4 before 2 5
#> 5 before 2 8
# The user can specify the length of a sequence.
xnew_data(data, xnew_seq(annual, .length_out = 3))
#> # A tibble: 3 × 3
#> period count annual
#> <fct> <int> <fct>
#> 1 before 2 1
#> 2 before 2 2
#> 3 before 2 3
# And only allow observed values.
xnew_data(data, xnew_seq(annual, .length_out = 3, .obs_only = TRUE))
#> # A tibble: 3 × 3
#> period count annual
#> <fct> <int> <fct>
#> 1 before 2 2
#> 2 before 2 3
#> 3 before 2 5
# With multiple variables all combinations are produced
xnew_data(data, period, xnew_seq(annual, .length_out = 3, .obs_only = TRUE))
#> # A tibble: 6 × 3
#> period count annual
#> <fct> <int> <fct>
#> 1 before 2 2
#> 2 before 2 3
#> 3 before 2 5
#> 4 after 2 2
#> 5 after 2 3
#> 6 after 2 5
# Naming a variable generates a new column of that name
xnew_data(data, Annual = annual)
#> # A tibble: 5 × 4
#> period count annual Annual
#> <fct> <int> <fct> <fct>
#> 1 before 2 1 1
#> 2 before 2 1 2
#> 3 before 2 1 3
#> 4 before 2 1 5
#> 5 before 2 1 8
# The new variable can be generated using an external vector, too
new_annual <- unique(data$annual)
xnew_data(data, Annual = new_annual)
#> # A tibble: 5 × 4
#> period count annual Annual
#> <fct> <int> <fct> <fct>
#> 1 before 2 1 1
#> 2 before 2 1 2
#> 3 before 2 1 3
#> 4 before 2 1 5
#> 5 before 2 1 8
# To only preserve observed combinations use
xnew_data(data, xobs_only(period, annual))
#> # A tibble: 4 × 3
#> period count annual
#> <fct> <int> <fct>
#> 1 before 2 2
#> 2 before 2 3
#> 3 after 2 5
#> 4 after 2 8
# And to cast the values use
xnew_data(data, xcast(annual = "3"))
#> # A tibble: 1 × 3
#> period count annual
#> <fct> <int> <fct>
#> 1 before 2 3
