Skip to contents

Turn a data.frame with date columns to a regular time series object if possible. Design to work with quarterly and monthly data.

Usage

df_to_reg_ts(
  dframe,
  var_cols,
  year_col = "year",
  period_col = "month",
  freq = 12,
  return_ts = T,
  by = NULL
)

Arguments

dframe

data.frame input

var_cols

columns that contain variables as opposed to date index.

year_col

integer, logical or character vector indicating the year position within the data.frame.

period_col

integer, logical or character vector indicating the period position within the data.frame.

freq

integer indicating the frequency of new time series.

return_ts

logical should a (list of) time series be returned? Defaults to TRUE. FALSE returns data.frame.

by

character overwrite automatically detected (from freq) by parameter. e.g. '1 day'. Defaults to NULL.

Examples

start_m <- as.Date("2017-01-01")
df_missing <- data.frame(
  date = seq(start_m, by = "2 months", length = 6),
  value = 1:6,
  another_value = letters[1:6],
  yet_another_col = letters[6:1]
)
df_to_reg_ts(df_missing, c("value", "another_value"))
#> $value
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
#> 2017  1   NA  2   NA  3   NA  4   NA  5   NA  6 
#> 
#> $another_value
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
#> 2017  a   NA  b   NA  c   NA  d   NA  e   NA  f 
#> 
df_to_reg_ts(df_missing, c("value", "another_value"), return_ts = FALSE)
#>          date value another_value
#> 1  2017-01-01     1             a
#> 2  2017-02-01  <NA>          <NA>
#> 3  2017-03-01     2             b
#> 4  2017-04-01  <NA>          <NA>
#> 5  2017-05-01     3             c
#> 6  2017-06-01  <NA>          <NA>
#> 7  2017-07-01     4             d
#> 8  2017-08-01  <NA>          <NA>
#> 9  2017-09-01     5             e
#> 10 2017-10-01  <NA>          <NA>
#> 11 2017-11-01     6             f