Reputation: 1079
I would like to create a time series object given a data set of heat consumption in which year, day and hour are in different columns. The file data looks like this
rdays heat[J] ds.jdate ds.hh ds.dow ds.tod ds.diy ds.wiy
0 1000 12965 2 6 2 182 26
0.0416 1150 12965 3 6 2 182 26
Whereas the explanation variables are:
rdays: 'Running days', No. of days from the start of the data set
(obs 1 = 0, obs 2 = 1/24, ...)
Information on time:
jdate: Julian date (No. of days since 1Jan1960)
hh: Time of day (0,...,23)
dow: Day of week (1=monday, ... , 7=sunday)
tod: Type of day (1=Working day, 2=Half-Holy (incl. Saturday),
3=Holy (incl. Sunday))
diy: Day in year
wiy: Week in year
The start period is 01Jul1995 02:00 and the end period is 30Jun1996 23:00
Upvotes: 1
Views: 303
Reputation: 132576
DF <- read.table(text="rdays heat[J] ds.jdate ds.hh ds.dow ds.tod ds.diy ds.wiy
0 1000 12965 2 6 2 182 26
0.0416 1150 12965 3 6 2 182 26 ",header=TRUE)
Calculate the seconds since 1960-01-01 and then use as.POSIXct
with the origin
parameter to get a date/time variable. Never forget to set the time zone when working with date/time variables.
DF$time <- as.POSIXct(DF$ds.jdate*24*3600+DF$ds.hh*3600,
origin=as.POSIXct("1960-01-01 00:00:00",tz="GMT"),tz="GMT")
Create an xts
time series for convenience.
library(xts)
as.xts(DF[,2,drop=FALSE],order.by=DF$time)
# heat.J.
# 1995-07-01 02:00:00 1000
# 1995-07-01 03:00:00 1150
You can use as.ts
on this if you want a ts
object.
Upvotes: 3