Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Time Series from a CSV file

Tags:

r

csv

I have data in the format Date, Time, Value. Here is a sample:

04/01/2010,07:10,17159
04/01/2010,07:20,4877
04/01/2010,07:30,6078
04/01/2010,07:40,3105
04/01/2010,07:50,4073
04/01/2010,08:00,6986
04/01/2010,08:10,7906
04/01/2010,08:20,7681
04/01/2010,08:30,5665
04/01/2010,08:40,6631
04/01/2010,08:50,4633
04/01/2010,09:00,6346
04/01/2010,09:10,6444
04/01/2010,09:20,6324
04/01/2010,09:30,11696
04/01/2010,09:40,7667
04/01/2010,09:50,6375
04/01/2010,10:00,5934
04/01/2010,10:10,12626
04/01/2010,10:20,11674
04/01/2010,10:30,4660
04/01/2010,10:40,3831
04/01/2010,10:50,7089
04/01/2010,11:00,4548
04/01/2010,11:10,2590
04/01/2010,11:20,3334
04/01/2010,11:30,5171

I want to convert this to a Time Series of Value keeping the same format. i.e. I need to be able store the date and time components too. This is is because i want to "deseasonalize" the data.

I have tried

z <- read.csv("fileName", header=TRUE,sep=",")

but not sure what to do from here. Can anyone show me how to load into a time series object properly? Or is there another way to do this?

Thanks in advance

like image 276
azuric Avatar asked Sep 16 '13 12:09

azuric


1 Answers

You can use the zoo package. The code below was writen to be reproducible but in actual practice text="Lines" would be replaced with file="fileName". Also as shown in the question the Date field is ambiguous and you may need to adjust the percent codes if its not day/month/year.

library(zoo)

Lines <- "Date,Time,Value
04/01/2010,07:10,17159
04/01/2010,07:20,4877
04/01/2010,07:30,6078
04/01/2010,07:40,3105
"

z <- read.zoo(text = Lines, sep = ",", header = TRUE, 
       index = 1:2, tz = "", format = "%d/%m/%Y %H:%M")

which gives:

> z
2010-01-04 07:10:00 2010-01-04 07:20:00 2010-01-04 07:30:00 2010-01-04 07:40:00 
              17159                4877                6078                3105 
like image 146
agstudy Avatar answered Sep 30 '22 19:09

agstudy