R/complete_ts.R

#' Plot for Missing Data Indicator
#'
#' Plot for Missing Data Indicator with class 'mi'
#' @param incomplete, Incomplete time series data which want to make complete one.
#' @param time_col, The column which contains the time information.
#' @param start, staring point, It supports heterogeneous format(see lubridate::ymd_hms).
#' @param end, see start above.
#' @param format, a character which function to be applied. It must be same format with start and end arguments.
#' @param tz, timezone. default = "Asia/Seoul".
#' @return Return to complete time series data with class 'mi'.
#' @export
#' @examples
#' Not ready to use.

complete_ts <- function (incomplete, time_col = 1, start, end, format, by, tz = Sys.timezone()) {

  if(!is.data.frame(incomplete)) stop("The class of the incomplete data is not a data.frame object")
  if(is.data.table(incomplete)){

    incomplete <- as.data.frame(incomplete)

  }

  if(!format %in% c("ymd", "ymd_h", "ymd_hm", "ymd_hms")){

    stop(cat("format error!\n",
             "choose one of these types: ymd, ymd_h, ymd_hm, ymd_hms\n",
             "see also: package 'lubridate'"))

  }

  stopifnot(format %in% c("ymd", "ymd_h", "ymd_hm", "ymd_hms"))

  time_col <- time_col

  ## define applying fucntion according to the format arguments

  if(format == "ymd"){

    applying_function <- ymd

  }else if(format == "ymd_h"){

    applying_function <- ymd_h

  }else if(format == "ymd_hm"){

    applying_function <- ymd_hm

  }else if(format == "ymd_hms"){

    applying_function <- ymd_hms

  }

  ti <- seq.POSIXt(applying_function(as.character(start), tz = tz),
                   applying_function(as.character(end), tz = tz),
                   by = by)

  df <- incomplete[, time_col] %>% as.character %>%
    applying_function(tz = tz) %>%
    data.frame(Time = ., incomplete[, -time_col])


  newdf <- zoo(df[, -1], df[, 1]) %>%
    merge(., zoo(x = NULL, ti), all = TRUE) %>%
    as.matrix

  attr(newdf, "class") <- c("matrix", "mi")
  newdf

}
Gi-Seop/ODA documentation built on Jan. 6, 2020, 12:49 p.m.