7
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ You are familiar with the tutorial at stat.pitt.edu/stoffer/tsa2/R_time_series_quick_fix.htm? Admittedly, it accompanies another package / book, but there may be some potential for carry over. $\endgroup$
    – caracal
    Commented Dec 6, 2010 at 19:44
  • $\begingroup$ Thanks for the help. I did see this and I think it is fantastic. I did edit my question to just focus on how to read in a univariate time series. Mainly, if I have the dates for my daily data, how can I construct a univariate time series in R dynamically? I have tried a few methods but they seem to include the date as another variable when I would only like to use it as a reference for start/end dates. Sorry, new to R and I am just trying to wrap my head around this. $\endgroup$
    – Btibert3
    Commented Dec 6, 2010 at 22:16

1 Answer 1

3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.