Skip to contents

Generates a new data frame (in the form of a tibble) with each variable held constant or varying as a unique ordered sequence. All possible unique combinations are included and the columns are in the same order as those in data.

Usage

new_data(
  data,
  seq = character(0),
  ref = list(),
  obs_only = list(character(0)),
  length_out = 30
)

Arguments

data

The data frame to generate the new data from.

seq

A character vector of the variables in data to generate sequences for.

ref

A named list of reference values for variables that are not in seq.

obs_only

A list of character vectors indicating the sets of variables to only allow observed combinations for. If TRUE then obs_only is set to be seq.

length_out

A count indicating the maximum length of sequences for all types of variables except logical, character, factor and ordered factors.

Value

A tibble of the new data.

Details

If an element of ref is a character vector and the corresponding column is a data frame, then the ref element is assigned the same factor levels as the column in the data. This is useful for choosing a factor level without having to set the correct levels.

See also

Examples

# an example data set
data <- tibble::tibble(
  vecint = c(1L, 3L),
  vecreal = c(1, 3),
  vecchar = c("b", "a"),
  vecdate = as.Date(c("2001-01-01", "2001-01-01"))
)

# vary count while holding other values constant
new_data(data, "vecint")
#> # A tibble: 3 × 4
#>   vecint vecreal vecchar vecdate   
#>    <int>   <dbl> <chr>   <date>    
#> 1      1       2 a       2001-01-01
#> 2      2       2 a       2001-01-01
#> 3      3       2 a       2001-01-01
# vary continual
new_data(data, "vecreal")
#> # A tibble: 30 × 4
#>    vecint vecreal vecchar vecdate   
#>     <int>   <dbl> <chr>   <date>    
#>  1      2    1    a       2001-01-01
#>  2      2    1.07 a       2001-01-01
#>  3      2    1.14 a       2001-01-01
#>  4      2    1.21 a       2001-01-01
#>  5      2    1.28 a       2001-01-01
#>  6      2    1.34 a       2001-01-01
#>  7      2    1.41 a       2001-01-01
#>  8      2    1.48 a       2001-01-01
#>  9      2    1.55 a       2001-01-01
#> 10      2    1.62 a       2001-01-01
#> # ℹ 20 more rows
new_data(data, c("vecchar", "vecint"))
#> # A tibble: 6 × 4
#>   vecint vecreal vecchar vecdate   
#>    <int>   <dbl> <chr>   <date>    
#> 1      1       2 a       2001-01-01
#> 2      2       2 a       2001-01-01
#> 3      3       2 a       2001-01-01
#> 4      1       2 b       2001-01-01
#> 5      2       2 b       2001-01-01
#> 6      3       2 b       2001-01-01