Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newdata helper #17

Open
m-muecke opened this issue Dec 31, 2024 · 1 comment
Open

newdata helper #17

m-muecke opened this issue Dec 31, 2024 · 1 comment

Comments

@m-muecke
Copy link
Member

m-muecke commented Dec 31, 2024

newdata helper function to generate new data analogous to https://tsibble.tidyverts.org/reference/new-data.html
Other references: https://github.com/poissonconsulting/newdata

@m-muecke
Copy link
Member Author

m-muecke commented Jan 11, 2025

Like in the readme could look for very simple cases something like the following. Note this currently only works with order column, but not with key column:

library(checkmate)

generate_newdata = function(task, n = 1L, resolution = "day") {
  assert_count(n)
  assert_string(resolution)
  assert_choice(
    resolution, c("second", "minute", "hour", "day", "week", "month", "quarter", "year")
  )

  order_cols = task$col_roles$order
  target = task$target_names
  max_index = max(task$data(cols = order_cols)[[1L]])

  unit = switch(resolution,
    second = "sec",
    minute = "min",
    hour = ,
    day = ,
    week = ,
    month = ,
    quarter = ,
    year = identity(resolution),
    stopf("Invalid resolution")
  )
  unit = sprintf("1 %s", unit)
  index = seq(max_index, length.out = n + 1L, by = unit)
  index = index[2:length(index)]

  newdata = data.table(index = index, target = rep(NA_real_, n))
  setnames(newdata, c(order_cols, target))
  setDF(newdata)[]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant