-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils-convert-to-ts.R
112 lines (101 loc) · 3.63 KB
/
utils-convert-to-ts.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#' Convert Data to Time Series Format
#'
#' @family Utility
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description This function converts data in a data frame or tibble into a time series format. It is designed to
#' work with data generated from `tidy_` distribution functions. The function can return time series data, pivot it
#' into long format, or both.
#'
#' @details
#' The function takes a data frame or tibble as input and processes it based on the specified options.
#' It performs the following actions:
#' 1. Checks if the input is a data frame or tibble; otherwise, it raises an error.
#' 2. Checks if the data comes from a `tidy_` distribution function; otherwise, it raises an error.
#' 3. Converts the data into a time series format, grouping it by "sim_number" and transforming the "y" column into a time series.
#' 4. Returns the result based on the chosen options:
#' - If `ret_ts` is set to TRUE, it returns the time series data.
#' - If `pivot_longer` is set to TRUE, it pivots the data into long format.
#' - If both options are set to FALSE, it returns the data as a tibble.
#'
#' @param .data A data frame or tibble to be converted into a time series format.
#' @param .return_ts A logical value indicating whether to return the time series data. Default is TRUE.
#' @param .pivot_longer A logical value indicating whether to pivot the data into long format. Default is FALSE.
#'
#' @examples
#' # Example 1: Convert data to time series format without returning time series data
#' x <- tidy_normal()
#' result <- convert_to_ts(x, FALSE)
#' head(result)
#'
#' # Example 2: Convert data to time series format and pivot it into long format
#' x <- tidy_normal()
#' result <- convert_to_ts(x, FALSE, TRUE)
#' head(result)
#'
#' # Example 3: Convert data to time series format and return the time series data
#' x <- tidy_normal()
#' result <- convert_to_ts(x)
#' head(result)
#'
#' @return
#' The function returns the processed data based on the chosen options:
#' - If `ret_ts` is set to TRUE, it returns time series data.
#' - If `pivot_longer` is set to TRUE, it returns the data in long format.
#' - If both options are set to FALSE, it returns the data as a tibble.
#'
#' @name convert_to_ts
NULL
#' @export
#' @rdname convert_to_ts
convert_to_ts <- function(.data, .return_ts = TRUE, .pivot_longer = FALSE){
# Data
df <- .data
# Attributes of incoming df
atb <- attributes(df)
# Checks
if (!is.data.frame(.data)){
rlang::abort(
message = "The .data argument requires a data.frame/tibble.",
use_cli_format = TRUE
)
}
if (!"tibble_type"%in% names(atb)){
rlang::abort(
message = "The data passed must come from a `tidy_` distribution function.",
use_cli_format = TRUE
)
}
# Variables
ret_ts <- as.logical(.return_ts)
pvt_long <- as.logical(.pivot_longer)
# Manipulation
df <- df |>
base::split(~ sim_number) |>
base::lapply(function(x) x[,"y"] |>
stats::ts())
df <- base::do.call(cbind, df)
# Return TS
if (ret_ts){return(df)}
# Return tibble
if (!ret_ts & !pvt_long){
df <- df |>
dplyr::as_tibble()
return(df)
}
# Return long tibble
if (!ret_ts & pvt_long & ncol(df) > 1){
df <- df |>
dplyr::as_tibble() |>
tidyr::pivot_longer(cols = tidyr::everything()) |>
dplyr::arrange(as.numeric(name)) |>
purrr::set_names("sim_number","y")
return(df)
}
if (!ret_ts & pvt_long & ncol(df) == 1){
df <- df |>
dplyr::as_tibble()
return(df)
}
}