[R] Zoo Data

Joshua Ulrich josh.m.ulrich at gmail.com
Tue Mar 12 20:53:55 CET 2013


It's much easier to use xts' time-of-day subsetting:

library(xts)
dat1 <- as.xts(read.zoo(text="TIME, Value1, Value2
01.08.2011 02:30:00, 4.4, 4.7
01.09.2011 03:00:00, 4.2, 4.3
01.11.2011 01:00:00, 3.5, 4.3
01.12.2011 01:40:00, 3.4, 4.5
01.01.2012 02:00:00, 4.8, 5.3
01.02.2012 02:30:00, 4.9, 5.2
01.08.2012 02:30:00, 4.1, 4.7
01.12.2012 03:00:00, 4.7, 4.3
01.01.2013 01:00:00, 3, 4.3
01.01.2013 01:30:00, 3.8, 4.1
01.01.2013 02:00:00, 3.8, 4.3
01.01.2013 02:30:00, 3.9, 4.2
01.01.2013 03:00:00, 3.7, 4.5
01.01.2013 03:30:00, 3.5, 4.1
01.02.2013 02:00:00, 3.8, 4.3
02.02.2013 04:30:00, 3.9, 4.2",
sep=",", header=TRUE, FUN=as.POSIXct, format="%d.%m.%Y %H:%M:%S"))

dat1["T02:30/T03:00"]

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com

R/Finance 2013: Applied Finance with R  | www.RinFinance.com


On Sat, Mar 9, 2013 at 11:59 AM, arun <smartpink111 at yahoo.com> wrote:
> HI Jakob,
>
> If your data is based on 30 min interval, this should work:
>
> dat1<-read.table(text="
> TIME, Value1, Value2
> 01.08.2011 02:30:00, 4.4, 4.7
> 01.09.2011 03:00:00, 4.2, 4.3
> 01.11.2011 01:00:00, 3.5, 4.3
> 01.12.2011 01:40:00, 3.4, 4.5
> 01.01.2012 02:00:00, 4.8, 5.3
> 01.02.2012 02:30:00, 4.9, 5.2
> 01.08.2012 02:30:00, 4.1, 4.7
> 01.12.2012 03:00:00, 4.7, 4.3
> 01.01.2013 01:00:00, 3, 4.3
> 01.01.2013 01:30:00, 3.8, 4.1
> 01.01.2013 02:00:00, 3.8, 4.3
> 01.01.2013 02:30:00, 3.9, 4.2
> 01.01.2013 03:00:00, 3.7, 4.5
> 01.01.2013 03:30:00, 3.5, 4.1
> 01.02.2013 02:00:00, 3.8, 4.3
> 02.02.2013 04:30:00, 3.9, 4.2
> ",sep=",",header=TRUE,stringsAsFactors=FALSE)
> dat1$TIME<-as.POSIXct(dat1$TIME,format="%d.%m.%Y %H:%M:%S")
>
> library(zoo)
> z1<- zoo(dat1[,-1],dat1[,1])
>
> Vec<-format(seq(from=as.POSIXct("2012-01-01 02:30:00", format="%Y-%m-%d %H:%M:%S"),length.out=3, by="30 min"),"%H:%M:%S")
> z1[format(time(z1),"%H:%M:%S")%in% Vec,]
> #                    Value1 Value2
> #2011-08-01 02:30:00    4.4    4.7
> #2011-09-01 03:00:00    4.2    4.3
> #2012-02-01 02:30:00    4.9    5.2
> #2012-08-01 02:30:00    4.1    4.7
> #2012-12-01 03:00:00    4.7    4.3
> #2013-01-01 02:30:00    3.9    4.2
> #2013-01-01 03:00:00    3.7    4.5
> #2013-01-01 03:30:00    3.5    4.1
> A.K.
>
>
>
>
> ----- Original Message -----
> From: Jakob Hahn <Jakob.Hahn at stud.hs-esslingen.de>
> To: arun <smartpink111 at yahoo.com>
> Cc:
> Sent: Saturday, March 9, 2013 10:04 AM
> Subject: Re: Zoo Data
>
> Hi Arun,
>
> z1[seq(which(time(z1)==as.POSIXct("**.**.2012 02:30:00",format="%d.%m.%Y %H:%M:%S")),which(time(z1)==as.POSIXct("**.**.2012 03:30:00",format="%d.%m.%Y %H:%M:%S"))),]
>
> Is there a possibility to get all data e.g between two times - not depending on a special date - all values betweend 2:30 and 3:30?
> Thanks
> Jakob
>
>
> ---
>
> Am 08.03.2013 um 22:05 schrieb arun:
>
>>
>>
>> Hi,
>> Try this:
>>  z1[seq(which(time(z1)==as.POSIXct("01.01.2012 02:00:00",format="%d.%m.%Y %H:%M:%S")),which(time(z1)==as.POSIXct("01.01.2013 03:00:00",format="%d.%m.%Y %H:%M:%S"))),]
>>
>> #                    Value1 Value2
>> #2012-01-01 02:00:00    4.8    5.3
>> #2012-02-01 02:30:00    4.9    5.2
>> #2012-08-01 02:30:00    4.1    4.7
>> #2012-12-01 03:00:00    4.7    4.3
>> #2013-01-01 01:00:00    3.0    4.3
>> #2013-01-01 01:30:00    3.8    4.1
>> #2013-01-01 02:00:00    3.8    4.3
>> #2013-01-01 02:30:00    3.9    4.2
>> #2013-01-01 03:00:00    3.7    4.5
>> A.K.
>>
>>
>> ----- Original Message -----
>> From: Jakob Hahn <Jakob.Hahn at gmail.com>
>> To: arun <smartpink111 at yahoo.com>
>> Cc:
>> Sent: Friday, March 8, 2013 3:40 PM
>> Subject: Re: Zoo Data
>>
>> Okay,
>> but works only by line not by date - if I don't know which line my date is;-)
>> Jakob
>>
>>
>> ----
>> Am 08.03.2013 um 21:32 schrieb arun:
>>
>>> HI Jakob,
>>>
>>> No problem.
>>>
>>> Your statement
>>> " Is it also possible plot from e.g. 01.01.2013 01:30:00 to 01.01.2013 02:00:00 - general spoken not the whole file?"
>>>
>>> So, I subset the "whole dataset" to show that you can do the plot for a specific subset of your data.
>>>
>>> Depending upon the number of labels and its spacing, you can change it:
>>> ix<- seq(1,length(tt), ....)
>>>
>>> Arun
>>>
>>>
>>>
>>> ----- Original Message -----
>>> From: Jakob Hahn <Jakob.Hahn at gmail.com>
>>> To: arun <smartpink111 at yahoo.com>
>>> Cc:
>>> Sent: Friday, March 8, 2013 3:27 PM
>>> Subject: Re: Zoo Data
>>>
>>> Thats great, thank you very much!!!
>>> Needed such a long time to find some stuff for this topic - I am a beginner but should work with R for my master thesis - so time is always against me;-)
>>> Have a nice day, regards
>>> Jakob
>>>
>>> PS: What do you mean with the subset z2?
>>>
>>>
>>> ---
>>> Am 08.03.2013 um 21:11 schrieb arun:
>>>
>>>>
>>>>
>>>> Hi Jakob,
>>>>
>>>> dat1<-read.table(text="
>>>> TIME, Value1, Value2
>>>> 01.08.2011 02:30:00, 4.4, 4.7
>>>> 01.09.2011 03:00:00, 4.2, 4.3
>>>> 01.11.2011 01:00:00, 3.5, 4.3
>>>> 01.12.2011 01:40:00, 3.4, 4.5
>>>> 01.01.2012 02:00:00, 4.8, 5.3
>>>> 01.02.2012 02:30:00, 4.9, 5.2
>>>> 01.08.2012 02:30:00, 4.1, 4.7
>>>> 01.12.2012 03:00:00, 4.7, 4.3
>>>> 01.01.2013 01:00:00, 3, 4.3
>>>> 01.01.2013 01:30:00, 3.8, 4.1
>>>> 01.01.2013 02:00:00, 3.8, 4.3
>>>> 01.01.2013 02:30:00, 3.9, 4.2
>>>> 01.01.2013 03:00:00, 3.7, 4.5
>>>> 01.01.2013 03:30:00, 3.5, 4.1
>>>> 01.02.2013 02:00:00, 3.8, 4.3
>>>> ",sep=",",header=TRUE,stringsAsFactors=FALSE)
>>>> dat1$TIME<-as.POSIXct(dat1$TIME,format="%d.%m.%Y %H:%M:%S")
>>>> library(zoo)
>>>> z1<- zoo(dat1[,-1],dat1[,1])
>>>>    plot(z1,plot.type="single",col=1:2,xaxt="n")
>>>> tt<- time(z1)
>>>> axis(side=1,at=tt,labels=FALSE)
>>>>    ix<- seq(1,length(tt),4) #can change here
>>>> fmt<- "%b-%Y"
>>>> labs<- format(tt,fmt)
>>>> axis(side=1,at=tt[ix],labels=labs[ix],tcl=-0.7,cex.axis=0.7)
>>>> legend("topleft",colnames(z1),lty=1,col=1:2)
>>>>
>>>> #subset
>>>> z2<- zoo(dat1[1:5,-1],dat1[1:5,1])
>>>>
>>>> Hope this helps.
>>>>
>>>> A.K.
>>>>
>>>>
>>>>
>>>>
>>>> ----- Original Message -----
>>>> From: "jakob.hahn at gmail.com" <jakob.hahn at gmail.com>
>>>> To: smartpink111 at yahoo.com
>>>> Cc:
>>>> Sent: Friday, March 8, 2013 10:05 AM
>>>> Subject: Zoo Data
>>>>
>>>> Hey Arun,
>>>> thank you so far, maybe you can tell me 2 more things:
>>>> 1st-how to deal with the xlim - right now R just makes me 2 dates on x-axis with Dez31 and Feb28 - how to define the interval.
>>>> 2nd-I am plotting multiple lines with col - is it possible to generate a legend with the name/header of the column from data?
>>>> Would be nice, if you could help me!
>>>> Thanks
>>>> Jakob
>>>>
>
> ______________________________________________
> 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