R/tsl_to_df.R

Defines functions tsl_to_df

Documented in tsl_to_df

#' Transform Time Series List to Data Frame
#'
#' @param tsl (required, list) Time series list. Default: NULL
#'
#' @return data frame
#' @export
#' @autoglobal
#' @examples
#'
#' tsl <- tsl_simulate(
#'   n = 3,
#'   rows = 10,
#'   time_range = c(
#'     "2010-01-01",
#'     "2020-01-01"
#'   ),
#'   irregular = FALSE
#' )
#'
#' df <- tsl_to_df(
#'   tsl = tsl
#' )
#'
#' names(df)
#' nrow(df)
#' head(df)
#' @family tsl_management
tsl_to_df <- function(
    tsl = NULL
){

  utils_check_args_tsl(
    tsl = tsl,
    min_length = 1
  )

  df_list <- lapply(
    X = tsl,
    FUN = function(x){

      data.frame(
        name = attributes(x)$name,
        time = zoo::index(x),
        as.data.frame(x)
      )

    }

  )

  df <- do.call(
    what = "rbind",
    args = df_list
  )

  rownames(df) <- NULL

  df

}

Try the distantia package in your browser

Any scripts or data that you put into this service are public.

distantia documentation built on April 4, 2025, 5:42 a.m.