
newdata is an R package to generate new data frames by
varying some variables while holding the others constant.
By default, all specified variables vary across their range while all other variables are held constant at a reference value. The user can specify the length of each sequence, require that only observed values and combinations are used and add new variables. Types, classes, factor levels and time zones are always preserved.
Consider the following observed old_data data frame.
library(newdata)
newdata::old_data
#> # A tibble: 3 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 TRUE 1 1 most most most 1970-01-02 1969-12-31 16:00:01 00'01"
#> 2 FALSE 4 4.5 most most most 1970-01-05 1969-12-31 16:00:04 00'04"
#> 3 NA 6 8.2 a rarity a rari… a ra… 1970-01-07 1969-12-31 16:00:06 00'06"By default all variables are set to a reference value.
xnew_data(old_data)
#> # A tibble: 1 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 3 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"The reference value depends on the class of the variable, by default:
The reference value of a variable can be modified with
xnew_value(), which passes additional arguments to
new_value().
xnew_data(old_data, xnew_value(dbl, .obs_only = TRUE))
#> # A tibble: 1 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 3 4.5 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"Specifying a variable causes it to vary sequentially across its range.
xnew_data(old_data, int)
#> # A tibble: 6 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 2 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 3 FALSE 3 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 4 FALSE 4 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 5 FALSE 5 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 6 FALSE 6 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"By default the sequence depends on the class of the variable:
These values can be overridden by setting the following options:
new_data.length_out_lgl, which is 2 by default, for
logical vectors (values greater than 2 are equivalent to 2);new_data.length_out_dbl, which is 30 by default, for
double vectors;new_data.length_out_int, which is 30 by default, for
integer, Date, POSIXct and hms vectors^1;new_data.length_out_chr, which is Inf by default, for
character, factor and ordered vectors.new_data.length_out_int as they are treated as integers for
the purpose of generating a sequence.When programming it is strongly recommended that the user explicitly specify the length of each sequence individually.
xnew_data(old_data, lgl, xnew_seq(int, .length_out = 3))
#> # A tibble: 6 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 3 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 3 FALSE 6 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 4 TRUE 1 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 5 TRUE 3 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 6 TRUE 6 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"A third alternative is to specify the length of all the sequences in the data set but this can result in less common character strings or later factor or ordered levels being dropped.
xnew_data(old_data, dbl, int, .length_out = 2)
#> # A tibble: 4 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 1 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 6 1 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 3 FALSE 1 8.2 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 4 FALSE 6 8.2 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"The user can also indicate whether only observed values should be used in the sequence.
xnew_data(old_data, xnew_seq(int, .length_out = 3, .obs_only = TRUE))
#> # A tibble: 3 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 4 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 3 FALSE 6 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"The xobs_only() function can be used to filter out
unobserved values after the sequence has been generated.
xnew_data(old_data, xobs_only(xnew_seq(int, .length_out = 3)))
#> # A tibble: 2 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 6 4.57 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"When two or more variables are specified all combinations are used.
xnew_data(old_data, int, fct)
#> # A tibble: 18 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 1 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 3 FALSE 1 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 4 FALSE 2 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 5 FALSE 2 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 6 FALSE 2 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 7 FALSE 3 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 8 FALSE 3 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 9 FALSE 3 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 10 FALSE 4 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 11 FALSE 4 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 12 FALSE 4 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 13 FALSE 5 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 14 FALSE 5 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 15 FALSE 5 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 16 FALSE 6 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 17 FALSE 6 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 18 FALSE 6 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"To only get the observed combinations use
xobs_only().
xnew_data(old_data, xobs_only(int, fct))
#> # A tibble: 3 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 4.57 most most a rari… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 4 4.57 most most a rari… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 3 FALSE 6 4.57 most a rarity a rari… 1970-01-04 1969-12-31 16:00:03 00'03"Modifying an existing variable or adding a new one is simple.
xnew_data(old_data, dbl = median(dbl, na.rm = TRUE), extra = c(TRUE, FALSE))
#> # A tibble: 2 × 10
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 3 4.5 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 3 4.5 most not obs a rarity 1970-01-04 1969-12-31 16:00:03 00'03"
#> # ℹ 1 more variable: extra <lgl>A modified or new variable takes the class of the values provided.
New values can be cast to the class of the original variable with
xcast().
xnew_data(old_data, xcast(lgl = 1, int = 7, dbl = 10L, fct = "a rarity", hms = "00:00:02"))
#> # A tibble: 1 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 TRUE 7 10 most a rarity a rari… 1970-01-04 1969-12-31 16:00:03 00'02"new_data() is a superseded wrapper on
xnew_data() that is provided for consistency with existing
code. It takes the variables to vary as a character vector and the
length of all the sequences as a count.
new_data(old_data, seq = c("int", "fct"), length_out = 5)
#> # A tibble: 15 × 9
#> lgl int dbl chr fct ord dte dtt hms
#> <lgl> <int> <dbl> <chr> <fct> <ord> <date> <dttm> <time>
#> 1 FALSE 1 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 2 FALSE 1 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 3 FALSE 1 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 4 FALSE 2 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 5 FALSE 2 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 6 FALSE 2 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 7 FALSE 3 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 8 FALSE 3 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 9 FALSE 3 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 10 FALSE 4 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 11 FALSE 4 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 12 FALSE 4 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 13 FALSE 6 4.57 most not obs a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 14 FALSE 6 4.57 most a rarity a rar… 1970-01-04 1969-12-31 16:00:03 00'03"
#> 15 FALSE 6 4.57 most most a rar… 1970-01-04 1969-12-31 16:00:03 00'03"To install the latest release version from CRAN.
install.packages("newdata")To install the latest development version from GitHub
# install.packages("pak")
pak::pak("poissonconsulting/newdata")or from r-universe.
install.packages("newdata", repos = c("https://poissonconsulting.r-universe.dev", "https://cloud.r-project.org"))Please report any issues.
Pull requests are always welcome.
Please note that the newdata project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.