stock_time.Rd
Add time dimensions to g3_stock classes
g3s_time_convert(year_or_time, step = NULL)
g3s_time(inner_stock, times, year = NULL, step = NULL)
Etiher vector of years, or vector of year & step strings, e.g. "1999-01".
Vector of years, used to generate times if provided.
Vector of steps, used to generate times if provided.
A g3_stock
that we extend with a time dimension
A vector of year/step integers as generated by g3s_time_convert
# 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