xnew_data: Generate New Data by Expansion

View source: R/xnew-data.R

xnew_dataR Documentation

Generate New Data by Expansion

Description

Generates a new data frame (in the form of a tibble)

Usage

xnew_data(.data, ..., .length_out = NULL)

Arguments

.data

The data frame to generate the new data from.

...

A list of variables to generate sequences for.

.length_out

NULL or a count specifying the maximum length of all sequences.

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.

See Also

xnew_value(), xnew_seq(), xcast() and xobs_only()

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)

# Specifying a variable causes it to vary across its range.
xnew_data(data, annual)

# The user can specify the length of a sequence.
xnew_data(data, xnew_seq(annual, length_out = 3))

# And only allow observed values.
xnew_data(data, xnew_seq(annual, length_out = 3, obs_only = TRUE))

# With multiple variables all combinations are produced
xnew_data(data, period, xnew_seq(annual, length_out = 3, obs_only = TRUE))

# To only preserve observed combinations use
xnew_data(data, xobs_only(period, annual))

# And to cast the values use
xnew_data(data, xcast(annual = "3"))

poissonconsulting/newdata documentation built on Jan. 18, 2024, 1:30 a.m.