[R-SIG-Finance] Dates manipulation
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Apr 29 11:15:56 CEST 2009
Just one follow up. Using the recent addition of the read.zoo
split= argument in the devel version of read.zoo we can
simplify this even further to:
> library(zoo)
>
> # grab devel version of read.zoo
> source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/R/read.zoo.R?rev=584&root=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-23 Y 10.15 "
>
> read.zoo(textConnection(Lines), header = TRUE, split = 2)
X Y
2009-04-21 2.32 10.20
2009-04-22 2.35 NA
2009-04-23 2.34 10.15
In the above split = 2 causes the rows to be split according to the conditioning
variable in column 2 and then merges them back together essentially
reshaping the input from long format to wide format (using the same terminology
used by the reshape command in the core of R).
On Mon, Apr 27, 2009 at 10:13 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> 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