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

Gabor Grothendieck ggrothendieck at gmail.com
Wed Feb 4 09:45:15 CET 2009


Without the code we can't tell what the problem is but
try this noting that ts is not normally used for daily data.
Using zoo package L is a list of time series and z
merges them into a multivariate series. Read the 3
zoo vignettes (pdf documents) that come with zoo:

Lines <-
"DATE          ,STOCK  ,RETURN-a ,RETURN-b
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"

library(zoo)
# DF <- read.csv("myfile.csv")
DF <- read.csv(textConnection(Lines))
f <- function(x) zoo(as.matrix(x[3:4]), as.Date(x$DATE, "%Y/%m/%d"))
L <- lapply(split(DF, DF$STOCK), f)
z <- do.call(merge, L)

On Wed, Feb 4, 2009 at 2:27 AM, Somani, Dinesh K
<dinesh.somani at gatech.edu> wrote:
> Hi
>
> 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
> analysis.
>
> 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
> Dinesh.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list