Solved – Getting started with time series in R

rtime series

With some great help from this forum, I have been able to get up and running with some basic time series analysis in R. Right now, my needs are mostly univariate time series.

Here is my question:

I can read in daily data from database into a data frame. I have two columns, date which is understood by R as POSIXct and the second which is the value of interest and numeric. What is the best, most straightforward way to make this a ts object where R understands the start/end dates and represents daily observations dynamically? It seems to me that I shouldn't be required to (when coercing my object to a ts object) manually tell it the start and end dates when the data frame is already has it.

For some context, I have been able to aggregate other data from daily to weekly, but find myself doing things in ways that just seem long and unnecessary considering R already understands my raw data as time. As you can tell, I am new to R and time series in R, but I figure that since R is so powerful, there probably is a pretty easy way around my issues.

Best Answer

It seems like you need the package xts. Create your time serie using

install.packages('xts')
library(xts)
X = xts(coredata(DF[,2]), order.by=DF[,1])

Then you will be able to manipulate your data easily.

to.weekly(X)  
to.monthly(X)

Please note that you will then manipulate xts objects and not ts. But no worries, you can go back to ts whenever needed.