[R] need help converting data.frame to time series

Somani, Dinesh K dinesh.somani at gatech.edu
Wed Feb 4 08:27:21 CET 2009


I am a new R user and need some help converting a data frame object to time series.

1. My input is a CSV file, contents something like these:
DATE          ,STOCK  ,RETURN-A ,RETURN-B, etc.
2009/02/02    ,A      ,0.01     ,0.011
2009/01/30    ,A      ,0.01     ,0.011
2009/01/29    ,A      ,0.01     ,0.011
2009/01/28    ,A      ,0.01     ,0.011
2009/02/02    ,B      ,0.01     ,0.011
2009/01/30    ,B      ,0.01     ,0.011
2009/01/29    ,B      ,0.01     ,0.011
2009/02/02    ,C      ,0.01     ,0.011
2009/01/30    ,C      ,0.01     ,0.011
2009/01/29    ,C      ,0.01     ,0.011
and so on, going down a few years. Notice that there are gaps in observation dates (weekends), and
also that some stocks do not have all the dates because there was no data that day (may be the stock
is newly listed, or de-listed, or something else.)

2. I have this loaded into a data.frame object using read.table(). Factor the STOCK names. Then split
the big table into N smaller data.frames one per STOCK name. The mode() for DATE and STOCK print
as numeric. (That somehow did not feel right, esp. STOCK). I have converted the DATE <- as.Date(DATE,"%Y/%m/%d")

3. I am now trying to create time series objects to perform my main work which is single-stock and pairwise

When I try to create from the data.frame a ts object, two strange things happen - one, the DATE shows 
going from 1 onwards, and STOCK is fixed to 1 (2 for Stock B, 3 for C, etc.).

I will greatly appreciate your help in resolving these issues, namely
- why am I losing DATE and STOCK
- how can I get a proper time series out of it

Thanks a lot

More information about the R-help mailing list