Add time dimensions to g3_stock classes

g3s_time_convert(year_or_time, step = NULL)

g3s_time(inner_stock, times, year = NULL, step = NULL)

Arguments

year_or_time

Etiher vector of years, or vector of year & step strings, e.g. "1999-01".

year

Vector of years, used to generate times if provided.

step

Vector of steps, used to generate times if provided.

inner_stock

A g3_stock that we extend with a time dimension

times

A vector of year/step integers as generated by g3s_time_convert

Value

g3s_time_convert

A single integer vector representing year and step.

If step is NULL, returns year, otherwise year * 1000 + step.

g3s_time

A g3_stock with an additional 'time' dimension.

If year/step provided, time is defined by those, otherwise times.

The g3_stock will not support iterating, only intersecting.

When intersecting with another stock, only do anything if cur_year and cur_step matches a time stored in the vector

Examples

library(magrittr)

# Define a stock with 3 lengthgroups and 3 years, not continuous
# When used, all steps within a year will be aggregated, year 2002 will be ignored.
stock <- g3_stock('name', c(1, 10, 100)) %>%
    g3s_time(year = c(2000, 2001, 2003))

# Use stock_instance to see what the array would look like
g3_stock_instance(stock)
#>          time
#> length    2000 2001 2003
#>   1:10      NA   NA   NA
#>   10:100    NA   NA   NA
#>   100:Inf   NA   NA   NA

# Define a stock with 3 lengthgroups and 3 years, 2 steps
# The dimension will have 6 entries, 2000.1, 2000.2, 2001.1, 2001.2, 2002.1, 2002.2
stock <- g3_stock('name', c(1, 10, 100)) %>%
    g3s_time(year = c(2000, 2001, 2002), step = 1:2)

# Use stock_instance to see what the array would look like
g3_stock_instance(stock)
#>          time
#> length    2000-01 2000-02 2001-01 2001-02 2002-01 2002-02
#>   1:10         NA      NA      NA      NA      NA      NA
#>   10:100       NA      NA      NA      NA      NA      NA
#>   100:Inf      NA      NA      NA      NA      NA      NA

# g3s_time_convert is best used with a data.frame
data <- read.table(header = TRUE, text = '
year step
2001 1
2001 2
# NB: No "2002 1"
2002 2
')
stock <- g3_stock('name', c(1, 10, 100)) %>%
    g3s_time(times = g3s_time_convert(data$year, data$step))

# Will also parse strings
g3s_time_convert(c("1999-01", "1999-02"))
#> [1] 199901 199902

# Use stock_instance to see what the array would look like
g3_stock_instance(stock)
#>          time
#> length    2001-01 2001-02 2002-02
#>   1:10         NA      NA      NA
#>   10:100       NA      NA      NA
#>   100:Inf      NA      NA      NA