Skip to contents

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.

Usage

convert_to_ts(.data, .return_ts = TRUE, .pivot_longer = FALSE)

Arguments

.data

A data frame or tibble to be converted into a time series format.

.return_ts

A logical value indicating whether to return the time series data. Default is TRUE.

.pivot_longer

A logical value indicating whether to pivot the data into long format. Default is FALSE.

Value

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.

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.

Author

Steven P. Sanderson II, MPH

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)
#> # A tibble: 6 × 1
#>        y
#>    <dbl>
#> 1  0.827
#> 2  1.12 
#> 3  0.256
#> 4  0.863
#> 5  1.35 
#> 6 -1.35 

# 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)
#> # A tibble: 6 × 1
#>        y
#>    <dbl>
#> 1 -1.23 
#> 2 -0.799
#> 3  0.406
#> 4  0.681
#> 5 -1.33 
#> 6 -1.41 

# Example 3: Convert data to time series format and return the time series data
x <- tidy_normal()
result <- convert_to_ts(x)
head(result)
#>               y
#> [1,]  0.4677781
#> [2,]  1.7446735
#> [3,] -0.6395092
#> [4,]  0.8658487
#> [5,] -0.1591997
#> [6,]  1.0443323