0

I have a csv data set that I would like t transform into time series data for time series analysis. The data looks like that (there are additional columns, and there are 17,190 obs.):

temp  interval

   10.0  2014-04-01 00:00:00

   10.0  2014-04-01 00:15:00

   10.0  2014-04-01 00:30:00

   10.0  2014-04-01 00:45:00

   7.8   2014-04-01 01:00:00

The Interval column is in POSIXct format.

I would appreciate help with the code for transforming it into time series please.

Thank you

Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 1
    Did you try anything? Googling your title gives [this as a first link](https://stackoverflow.com/questions/29046311/how-to-convert-data-frame-into-time-series-in-r) – Sotos Jan 23 '19 at 08:41
  • yes, I did try some stuff including trying to use zoo package. Also, I tried what is described in the link you provided, but it converts the "interval" column into numbers that I can't quite understand. – Zohar Eylen Jan 23 '19 at 08:46
  • Can you share the output of `dput(head(your_data))`? – markus Jan 23 '19 at 09:00
  • 1
    In zoo, I think it's just `zoo(data)`, but your `data$interval` column for the time has to come first. E.g. - `read.zoo(dat[c("interval","temp")], drop=FALSE)` as per https://stackoverflow.com/questions/14064097/r-convert-between-zoo-object-and-data-frame-results-inconsistent-for-different – thelatemail Jan 23 '19 at 09:11

2 Answers2

0

CSV stands for comma separated values. The data shown in the question is not in that form but if we assume that the data is a data frame DF shown reproducibly in the Note at the end then the following code gives a zoo series z and also converts it to a ts series tt where the times are the number of seconds since 1970-01-01 00:00:00. See ?read.zoo for more information on that function. Also the zoo package contains an entire vignette with many read.zoo examples.

z can be used for plotting and tt might be useful if you are using functions that only accept ts class input.

library(zoo)
z <- read.zoo(DF, index = "interval", tz = "")
tt <- as.ts(z)

Note

Lines <- "
temp  interval
   10.0  2014-04-01 00:00:00
   10.0  2014-04-01 00:15:00
   10.0  2014-04-01 00:30:00
   10.0  2014-04-01 00:45:00
   7.8   2014-04-01 01:00:00"

# read into separate lines, trim whitespace from ends and
#  replace 2 or more consecutive spaces with comma
L <- gsub("  +", ",", trimws(readLines(textConnection(Lines))))
DF <- read.csv(text = L)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

If you read in the csv with read_csv in tidyverse you'll get the interval column in POSIXct class automatically.

dput below:

library(tidyverse)

df <- structure(list(temp = c(10, 10, 10, 10, 7.8), interval = structure(c(1396310400, 
1396311300, 1396312200, 1396313100, 1396314000), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L), spec = structure(list(
cols = list(temp = structure(list(), class = c("collector_double", 
"collector")), interval = structure(list(format = ""), class = c("collector_datetime", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))

you can then just reorder cols and convert to zoo.

library(zoo)

df <- df %>% 
  select(interval, temp) %>% 
  zoo()

class(df)
[1] "zoo"
nycrefugee
  • 1,629
  • 1
  • 10
  • 23