[R-SIG-Finance] data handling with timeSeries
Yohan Chalabi
chalabi at phys.ethz.ch
Wed Dec 3 18:30:42 CET 2008
>>>> "JK" == J Konnonen <jkonn2001 at yahoo.com>
>>>> on Wed, 3 Dec 2008 06:45:45 -0800 (PST)
JK> Dear All,
JK>
JK> I met some problems with data handling when using the timeSeries library
JK>
JK> I have some csv files, each containing two columns, first column are the series of dates, second are the series prices
JK>
JK> I read in all files using, and create a matrix with the dates in the first column, and the calculated returns in the remaining columns
JK>
JK> > library(timeSeries)
JK>
JK> > returnsdata <- read.csv("foo1.csv",header=T,sep=",")
JK> > attach(returnsdata)
JK> > foo1 = as.timeSeries(returnsdata)
JK> > foo1bind <- returns(window(foo1, start=2007-01-01, end=2007-12-31))
JK>
JK> > returnsdata <- read.csv("foo2.csv",header=T,sep=",")
JK> > attach(returnsdata)
JK> > foo2 = as.timeSeries(returnsdata)
JK> > foo2bind <- returns(window(foo2, start=2007-01-01, end=2007-12-31))
JK>
JK> > ralls <- cbind(foo1bind,foo2bind)
JK> > colnames(ralls) <- c("FOO1","FOO2")
JK>
JK> And then I would like to have the last 1000 lines of this matrix for some computations
JK>
JK> > index <- ... #some value
JK> > past1000 <- ralls[(index-1000):(index-1)]
you are missing the comma ! This is why you get back a vector. This is
the same behaviour as with a matrix. Without a comma you do not know if
you subset in the rows and columns.
I suppose you wanted to use
ralls[(index-1000):(index-1),]
Note you could use the function tail()
JK>
JK> But
JK> > print(past1000)
JK> shows that past1000 doesn't contain either the column of dates, nor the header line, only the columns of returns
JK>
JK> Also
JK> > print(is.timeSeries(past1000))
JK> returns FALSE, and although I attempted several conversions on past1000, which resulted this check to be TRUE, the effective content of past1000 didn't seem to be changed, still no dates were printed
JK>
JK> And despite
JK> > print(is.timeSeries(ralls))
JK> returns TRUE, and printing the whole ralls object contains the dates column and the header
JK> > print(ralls at .Data[,1])
JK> returns only the second column of ralls (that is, the first column of returns) and no date column is displayed. This seems to indicate that perhaps there is already some problem with ralls, rooted possibly in the datafile reading and binding?
JK>
JK> The expectation would be to have past1000 an object where the first column contains the dates, and the remaining columns contain the returns, but I couldn't figure it out so far.
JK>
JK> Any suggestion is very much appreciated.
JK>
JK> Best regards,
JK> /Johan
JK>
JK> _______________________________________________
JK> R-SIG-Finance at stat.math.ethz.ch mailing list
JK> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
JK> -- Subscriber-posting only.
JK> -- If you want to post, subscribe first.
--
PhD student
Swiss Federal Institute of Technology
Zurich
www.ethz.ch
More information about the R-SIG-Finance
mailing list