Patty
Patty

Reputation: 167

Transforming date format in R to something more manageable for time series data?

I have imported some time series data into R which looks like this.

> data1[1:2,]
             X1          X2      X3
1   01/01/04 07:47:52  1.1111  1.1112
2   01/01/04 17:46:14  1.1112  1.1113

I want to work with this data as a time series, however I'm unsure how to deal with the dates. The data is not reported in regular intervals, they vary widely, so simply plotting the data by index transforms how the data actually appears. The data has approx 1 million rows.

I was considering transforming the date column to fractional numbers, but it seems overly complex and I'm sure there's a built in R package that can handle this better.

Specifically there's two things I'd like to do here:

  1. The first is break up the first column into one comprised of the day/month/year and a second comprised of hour:minute:second

  2. The second is find an R package which facilitates transforming dates and/or times into discrete indexes which doesn't transform the data. So that the difference between indexes of two rows depends on the difference in time between them (instead of just going up one index each row).

Alternatively (to 2), a package which lets me plot time series against date format data and accurately spaces data points would also be a huge help.

Upvotes: 0

Views: 89

Answers (2)

neilfws
neilfws

Reputation: 33792

To answer the second part, ggplot2 will handle dates and times automatically if they are in the correct format.

library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)

data <- data.frame(X1 = c("01/01/04 07:47:52", "01/01/04 17:46:14", "01/02/04 12:01:17"),
                   X2 = c(1.1111, 1.1112, 1.1113),
                   X3 = c(1.1112, 1.1113, 1.1114))

data %>% 
  mutate(dt = dmy_hms(X1)) %>% 
  gather(variable, value, -X1, -dt) %>% 
  ggplot(aes(dt, value)) + 
    geom_line(aes(color = variable, group = variable))

enter image description here

Upvotes: 2

lebelinoz
lebelinoz

Reputation: 5068

This sounds like an opinion-based question so here goes: I think the lubridate package is best for making a non-issue out of date conversion. You can convert your date string to dates using dmy, into time using hms and into a date-time combination using something like dmy_hms.

Upvotes: 2

Related Questions