[R-SIG-Finance] Dates manipulation
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Apr 27 16:13:56 CEST 2009
zoo objects represent time series and that is not a time series
in its current form but you could read it in using read.table and
then its only one line of code to transform it to one.
library(zoo)
Lines <-
"Date Ticker Price
2009-04-21 X 2.32
2009-04-22 X 2.35
2009-04-23 X 2.34
2009-04-21 Y 10.2
2009-04-22 Y 10.32
2009-04-23 Y 10.15"
DF <- read.table(textConnection(Lines), header = TRUE)
DF$Date <- as.Date(DF$Date)
do.call(merge, by(DF[-2], DF[2], function(d) zoo(d$Price, d$Date)))
# Using read.zoo from the development version of zoo
# the last line could even be reduced to this:
source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/R/read.zoo.R?rev=519&root=zoo")
do.call(merge, by(DF[-2], DF[2], read.zoo))
Make sure you have a sufficiently recent version of zoo since
read.zoo
On Mon, Apr 27, 2009 at 9:34 AM, Manoj <manojsw at gmail.com> wrote:
> Hi All,
> I am using version 2,8,1 and working with zoo library.
>
> The sample/dummy data-set i am working off takes the following structure:
>
> Date Ticker Price
> 2009-04-21 X 2.32
> 2009-04-22 X 2.35
> 2009-04-23 X 2.34
> 2009-04-21 Y 10.2
> 2009-04-22 Y 10.32
> 2009-04-23 Y 10.15
>
> The data-frame consist of Date as a Date object, Ticker and Price.
> Price can be uniquely identified by Date & Ticker.
>
> One of the issues with creating a zoo object with above data-set is
> the fact that the unique key is basically a composite key so it's not
> purely time-series data but does take the format of panel data.
>
> I want to be able to perform normal data function (lag,time etc) on
> the above data-set for example, lag would return all the rows except
> for ticker X&Y for 2009-04-23.
>
> Is there anyway, i can sub-class zoo object to achieve this functionality?
>
> Any suggestions would be greatly appreciated - thanks in advance.
>
>
> Manoj
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
More information about the R-SIG-Finance
mailing list