
A generator that gives one row per combination of world elements
Source:R/synthetic_grid.R
synthetic_grid_generator.RdReturns a function(profile, n, seed, world) for the generator of a
synthetic spec. The function gives one row for each combination of the
world elements in grid, and draws the other columns with
csdb::simulate_from_profile().
Details
The generator works in this order:
It stops when
worldisNULLor does not hold every element ofgrid.It takes one unit from each element of
grid. A vector is one column of unique values. A data.frame gives its columns that are innames(profile$field_types), as unique rows, so the values in one row stay together. An element with no column in the table is skipped.It crosses the units.
data.table::CJ()crosses the vectors, in the order ofgrid, and each data.frame is then crossed with every row so far. The grid is the result.It calls
csdb::simulate_from_profile()fornrow(grid)rows, withseed. It removesprofile$keys, because the grid makes the rows unique. It removes every data.frame element from the world. csdb draws wrong values for a data.frame element named like a TEXT column, and stops for one named like an INTEGER column.It replaces each grid column of the draw with the grid.
It calls
finish(d, seed, world)whenfinishis notNULL.worldis the world as the generator got it, with its data.frame elements.It orders the columns as
names(profile$field_types), and returns the data.table.
The generator ignores n: the grid sets the number of rows. For a
partitioned table, put the partition column in the world and in grid,
so that every partition gets every combination.
The generator stops when the elements of grid give no column of the
table, or give one column twice. It installs the grid values with their R
types, so a world element MUST have the R type of its column.
synthetic_weeks() and synthetic_years() give the time columns with
those types.
Examples
generator <- synthetic_grid_generator(c("location", "age", "weeks"))
world <- list(
location = data.frame(
location_code = c("nation_nor", "county_nor03"),
granularity_geo = c("nation", "county")
),
age = c("total", "000_004"),
weeks = synthetic_weeks("2024-01", "2024-03")
)
profile <- csdb::profile_schema(
c(location_code = "TEXT", granularity_geo = "TEXT", age = "TEXT",
isoyearweek = "TEXT", n = "INTEGER"),
keys = c("location_code", "age", "isoyearweek")
)
generator(profile, n = NULL, seed = 1L, world = world)
#> location_code granularity_geo age isoyearweek n
#> <char> <char> <char> <char> <int>
#> 1: nation_nor nation total 2024-01 38
#> 2: nation_nor nation total 2024-02 50
#> 3: nation_nor nation total 2024-03 41
#> 4: county_nor03 county total 2024-01 5
#> 5: county_nor03 county total 2024-02 23
#> 6: county_nor03 county total 2024-03 31
#> 7: nation_nor nation 000_004 2024-01 13
#> 8: nation_nor nation 000_004 2024-02 1
#> 9: nation_nor nation 000_004 2024-03 44
#> 10: county_nor03 county 000_004 2024-01 17
#> 11: county_nor03 county 000_004 2024-02 21
#> 12: county_nor03 county 000_004 2024-03 77