Introducing TidyDensity’s New Powerhouse: The convert_to_ts() Function


Steven P. Sanderson II, MPH


November 3, 2023


If you’re an R enthusiast like me, you know that data manipulation is at the core of everything we do. The ability to transform your data swiftly and efficiently can make or break your data analysis projects. That’s why I’m thrilled to introduce a game-changing function in TidyDensity, my very own R library. Say hello to convert_to_ts()!

In the world of data analysis, time series data is like a treasure chest of insights waiting to be unlocked. Whether you’re tracking stock prices, monitoring patient data, or analyzing the temperature over the years, having your data in a time series format is a crucial step in the process. With convert_to_ts(), that process just got a whole lot easier.

The Basics

Let’s start with the basics. The syntax of convert_to_ts() is straightforward:

convert_to_ts(.data, .return_ts = TRUE, .pivot_longer = FALSE)
  • .data: This is your data, the data frame or tibble you want to convert into a time series format. It’s the heart of your analysis.

  • .return_ts: A logical value that lets you decide whether you want to return the time series data. By default, it’s set to TRUE, which is usually what you’ll want.

  • .pivot_longer: Another logical value that determines whether you want to pivot the data into long format. By default, it’s set to FALSE, but you can change that if needed.

The Magic of convert_to_ts()

So, what exactly does convert_to_ts() do, and why is it a game-changer? Imagine you have a data frame with time-based data in a wide format. You’ve got columns representing different time points, and you want to transform it into a time series format for easier analysis. This is where convert_to_ts() steps in.

By simply passing your data frame as the .data argument, convert_to_ts() does the heavy lifting for you. It reshapes your data into a tidy time series format, making it easier to work with and analyze. If you set .return_ts to TRUE, it will return the time series data, ready for your next analysis step.

But that’s not all. Sometimes, you might want to pivot your data into long format for specific analyses or visualizations. That’s where the .pivot_longer argument comes into play. If you set it to TRUE, convert_to_ts() will pivot your data into long format, providing you with even more flexibility in your data manipulation.

Real-World Applications

Let’s talk about the real-world applications of convert_to_ts(). Consider you are working with some time series data and it follows some distribution fairly well. You may want to run multiple simulations of that data, which can be done with one of the tidy_ distribution functions, and you can take that output and pipe it right into convert_to_ts() and see different simulations of a time series generated from some distribution. I do this on a regular basis at my day job in healthcare.

But it’s not just limited to healthcare. Stock analysts, meteorologists, and anyone dealing with time-based data can benefit from this versatile function. The possibilities are endless, and the power is in your hands.


Example 1: Convert data to time series format without returning time series data


x <- tidy_normal()
result <- convert_to_ts(x, FALSE)
# A tibble: 6 × 1
1  1.23  
2  0.715 
3  0.738 
4  1.08  
5 -0.0613
6  1.19  

Example 2: Convert data to time series format and pivot it into long format

x <- tidy_normal(.num_sims = 4)
result <- convert_to_ts(x, FALSE, TRUE)
# A tibble: 6 × 2
  sim_number      y
  <chr>       <dbl>
1 1           0.881
2 1          -0.105
3 1          -0.655
4 1          -0.564
5 1           0.600
6 1          -0.811
[1] "1" "2" "3" "4"
convert_to_ts(x, TRUE, TRUE) |> head()
              1           2          3          4
[1,]  0.8807494  0.04680495 -0.1993147 -1.5549585
[2,] -0.1045016  0.14023102  0.5433512 -1.9247656
[3,] -0.6549336 -0.20818900 -0.1689992 -0.2934131
[4,] -0.5638595  0.87588361  0.4802693 -1.2052377
[5,]  0.6002684  0.26137176  1.5993445 -0.5379518
[6,] -0.8111576 -0.60834621 -0.4859808 -0.2178982

Example 3: Convert data to time series format and return the time series data

x <- tidy_normal()
result <- convert_to_ts(x)
[1,] -0.21509987
[2,] -0.88989659
[3,]  0.69464989
[4,] -0.03296698
[5,] -0.82499955
[6,] -0.71676037


In the ever-evolving world of data analysis, having the right tools at your disposal is crucial. convert_to_ts() in TidyDensity is one such tool that can simplify your data transformation processes and elevate your data analysis game. It’s all about efficiency and flexibility, allowing you to focus on what matters most – deriving valuable insights from your data.

So, whether you’re a data enthusiast, a coding wizard, or someone curious about the world of R, convert_to_ts() is here to make your life easier. Give it a try, explore its capabilities, and unlock the potential of your time series data. With TidyDensity, the possibilities are endless, and your data analysis journey just got a whole lot smoother. Happy coding!