R/power_market.R

Defines functions c2_phs_of_date df_with_c2_phs df_with_peak format_spot

#' @export
c2_phs_of_date <- function(date) {
  mo <- month(date)
  wd <- wday(date, week_start = 1)
  hr <- hour(date) + 1

  seas <- "E"
  htype <- "HC"
  if (hr %in% 7:22) {
    htype <- "HP"
  }
  if (mo %in% c(1:3, 11:12)) {
    seas <- "H"
    if ((hr %in% c(10,11,19,20)) & (wd != 7)) {
      seas <- ""
      htype <- "Pte"
    }
  }
  phs <- paste0(htype, seas)
  phs
}

#' @export
df_with_c2_phs <- function(df, date_col="date") {
  df$phs <- sapply(df$date, c2_phs_of_date)
  df
}

#' @export
df_with_peak <- function(df, date_col="date") {
  dfm <- df %>%
    dplyr::mutate(
      yr = year(df[,date_col]),
      qr = quarter(df[,date_col]),
      mo = month(df[,date_col]),
      wd = wday(df[,date_col], week_start = 1),
      hr = hour(df[,date_col]) + 1,
      tl = ifelse((hr %in% 9:20) & (!wd %in% 6:7), "Peak", "Offpeak") %>% as.factor()
    )
  dfm
}

#' @export
format_spot <- function(spot, date_format="%Y-%m-%d") {
  spot %>%
    gather(key = "hour", value = "price", H0:H23) %>%
    mutate(dt = with_tz(
      as.POSIXct(paste(date, hour), format = paste(date_format, "H%H"),
                 tz = "Europe/Paris"),
      Sys.getenv("TZ")
    )) %>%
    select(dt, price, -date, -hour) %>%
    arrange(dt) %>%
    na.omit() %>%
    distinct(dt, .keep_all = T)
}
vwrobel/dataexpr documentation built on Aug. 9, 2019, 8:44 a.m.