R/fun_toPeriodCl.R

#' fun_toPeriodCl
#'
#' @param df Dataframe with columns Price, mean, and sd
#' @param period Period one want to convert data to. One can choose c("minutes", "hours", "days", "weeks", "months", "quarters", "years") (OBS: one can't convert days to minutes)
#'
#' @return Dataframe with periods specified data and the same columns as input. One closing prices are returned
#' @export
#'
fun_toPeriodCl <- function(df, period = c("minutes", "hours", "days", "weeks", "months", "quarters", "years")) {
  df <- df[-unique(which(is.na(df), arr.ind = TRUE)[,1]),]
  # df <- df[-which(is.na(df[,2])),]
  out.df <- apply(df, 2, function(x) {
    xts::to.period(x, period = period) %>%
      quantmod::Cl()
  })
  if(any(is.na(out.df))) {
    out.df <- out.df[-unique(which(is.na(out.df), arr.ind = TRUE)[,1]),]
  }
  return(out.df)
}
3schwartz/SpecialeScrAndFun documentation built on May 4, 2019, 6:29 a.m.