Date calculations

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Date calculations

henaffp
I'm a rank beginner in R and R-metrics, but experienced in matlab and other languages. I'm strugling with date arithmetics in R. I'm doing statistical analysis on financial time series, using its, which uses POSIXct objects to measure time. What is the best way to perform simple date arithmetic with these objects? I do not care about h/m/s, nor do I care about daylight saving time. Example of a problem I faced:

Add one day to 3-apr-2005. (daylight saving starts on 4-apr-2005).
If I do
> as.POSIXct("2005-04-03") + 86400
I get 1 a.m. on the 4th of april.

So, I do something like
> dt1 <- as.POSIXct("2005-04-03")
> x1 <- timeDate(dt1)
> x2 <- timeNdayOnOrAfter(x1, n=2)
> dt2 <- as.POSIXct(as.POSIXlt(x2))

There has to be something simpler!

Another problem. The tseries and timeDate packages seem to require that one changes his timezone environment variable to GMT. This is not really possible in a corporate environment. Is there a work-around?

Any help will be much appreciated.

Patrick Henaff

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Date calculations

Diethelm Wuertz
P. Hénaff wrote:

>I'm a rank beginner in R and R-metrics, but experienced in matlab and other languages. I'm strugling with date arithmetics in R. I'm doing statistical analysis on financial time series, using its, which uses POSIXct objects to measure time. What is the best way to perform simple date arithmetic with these objects? I do not care about h/m/s, nor do I care about daylight saving time. Example of a problem I faced:
>
>Add one day to 3-apr-2005. (daylight saving starts on 4-apr-2005).
>If I do
>  
>
>>as.POSIXct("2005-04-03") + 86400
>>    
>>
in Rmetrics use:

myFinCenter = "GMT" # for daily dates
timeDate("2005-04-03")
[1] "GMT"
[1] [2005-04-03]
timeDate("2005-04-03") + 3*24*3600
[1] "GMT"
[1] [2005-04-06]

# Living in Zurich
myFinCenter = "Zurich"
timeDate("2005-04-03")
[1] "Zurich"
[1] [2005-04-03 02:00:00]
timeDate("2005-04-03") + 3*24*3600
[1] "Zurich"
[1] [2005-04-06 02:00:00]

# Use for Daily dates:
timeDate("2005-04-03", zone = "Zurich")
[1] "Zurich"
[1] [2005-04-03]
timeDate("2005-04-03", zone = "Zurich") + 3*24*3600
[1] "Zurich"
[1] [2005-04-06]


>I get 1 a.m. on the 4th of april.
>
>So, I do something like
>  
>
>>dt1 <- as.POSIXct("2005-04-03")
>>x1 <- timeDate(dt1)
>>x2 <- timeNdayOnOrAfter(x1, n=2)
>>dt2 <- as.POSIXct(as.POSIXlt(x2))
>>    
>>
>
>There has to be something simpler!
>
>Another problem. The tseries and timeDate packages seem to require that one changes his timezone environment variable to GMT. This is not really possible in a corporate environment. Is there a work-around?
>  
>
It is no longer necessary to specify the timezone. This holds since the
latest
version 221.10065 (download it from CRAN).
For daily dates just be sure that the time zone where you recorded the data
(zone=...) and the time zone where you use your data (FinCenter=..) are
specified as the same. both GMT, or the financial Center where you are
living and your operating system's time zone environment variable is set.
(Note, this is not necessarily the same timezone your clock works on your
windows box, which doesn't make use of the Windows time zone environment
variable TZ).

Please let me know if you have further questions (or problems) with
timeDate()
Diethelm

>Any help will be much appreciated.
>
>Patrick Henaff
>
> [[alternative HTML version deleted]]
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Date calculations

David Kane
In reply to this post by henaffp
> I'm a rank beginner in R and R-metrics, but experienced in matlab and
> other languages. I'm strugling with date arithmetics in R. I'm doing
> statistical analysis on financial time series, using its, which uses
> POSIXct objects to measure time. What is the best way to perform simple
> date arithmetic with these objects? I do not care about h/m/s, nor do I
> care about daylight saving time. Example of a problem I faced:

Perhaps date() is what you want. That's what we use for financial
applications in which h/m/s does not matter.

Dave Kane

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Date calculations

Achim Zeileis
In reply to this post by henaffp
On Wed, 22 Mar 2006 00:38:20 -0600 P. Hénaff wrote:

> I'm a rank beginner in R and R-metrics, but experienced in matlab and
> other languages. I'm strugling with date arithmetics in R. I'm doing
> statistical analysis on financial time series, using its, which uses
> POSIXct objects to measure time. What is the best way to perform
> simple date arithmetic with these objects? I do not care about h/m/s,
> nor do I care about daylight saving time.

In that case I would recommend to go with the "Date" class available in
base R, e.g.
  as.Date("2005-04-03") + 0:10
which should be rather self-explanatory.

The package zoo provides time series infrastructure where the time
information can be stored in "Date" (or virtually any other class).
  library("zoo")
  z <- zoo(rnorm(11), as.Date("2005-04-03") + 0:10)
  plot(z)

hth,
Z

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Date calculations

Brandt, T. (Tobias)
In reply to this post by henaffp
In addition to what's said already, I would just like to point out the very
useful R Help Desk article "Date and Time classes in R" in R News June 2004
by Gabor Grothendieck and Thomas Petzoldt.

>-----Original Message-----
>From: [hidden email]
>[mailto:[hidden email]] On Behalf Of
>Achim Zeileis
>Sent: 22 March 2006 02:19 PM
>To: P. Hénaff"
>Cc: [hidden email]
>Subject: Re: [R-sig-finance] Date calculations
>
>On Wed, 22 Mar 2006 00:38:20 -0600 P. Hénaff wrote:
>
>> I'm a rank beginner in R and R-metrics, but experienced in
>matlab and
>> other languages. I'm strugling with date arithmetics in R. I'm doing
>> statistical analysis on financial time series, using its, which uses
>> POSIXct objects to measure time. What is the best way to perform
>> simple date arithmetic with these objects? I do not care
>about h/m/s,
>> nor do I care about daylight saving time.
>
>In that case I would recommend to go with the "Date" class
>available in base R, e.g.
>  as.Date("2005-04-03") + 0:10
>which should be rather self-explanatory.
>
>The package zoo provides time series infrastructure where the
>time information can be stored in "Date" (or virtually any
>other class).
>  library("zoo")
>  z <- zoo(rnorm(11), as.Date("2005-04-03") + 0:10)
>  plot(z)
>
>hth,
>Z
>
>_______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
********************
Nedbank Limited Reg No 1951/000009/06. The following link displays the names of the Nedbank Board of Directors and Company Secretary. [ http://www.nedbank.co.za/terms/DirectorsNedbank.htm ]
This email is confidential and is intended for the addressee only. The following link will take you to Nedbank's legal notice. [ http://www.nedbank.co.za/terms/EmailDisclaimer.htm ]
********************

        [[alternative HTML version deleted]]


_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Date calculations

Diethelm Wuertz
In reply to this post by henaffp
Do you have the latest version of Rmetrics 221.10065 ?  Please update
Rmetrics and try again.

DW

Henaff, Patrick (MLCI) wrote:

>Thanks for the answer to my previous post. Somehow, I cannot replicate
>your example (see below). Note that z + one day ends up at 04-apr-2005
>at 1 a.m.  
>
>  
>
>>R.version
>>    
>>
>         _              
>platform i386-pc-mingw32
>arch     i386          
>os       mingw32        
>system   i386, mingw32  
>status                  
>major    2              
>minor    2.1            
>year     2005          
>month    12            
>day      20            
>svn rev  36812          
>language R  
>            
>  
>
>>myFinCenter="GMT"
>>z <- timeDate("2005-04-03",zone="GMT", FinCenter=myFinCenter)
>>    
>>
>Warning message:
>Set timezone to GMT! in: timeDate("2005-04-03", zone = "GMT", FinCenter
>= myFinCenter)
>  
>
>>z+86400
>>    
>>
>[1] "GMT"
>[1] [2005-04-04 01:00:00]
>Warning messages:
>1: Set timezone to GMT! in: "+.timeDate"(z, 86400)
>2: Set timezone to GMT! in: timeDate(e1, zone = e1@FinCenter, FinCenter
>= "GMT")
>3: Set timezone to GMT! in: timeDate(e1GMT + e2, zone = "GMT", FinCenter
>= e1@FinCenter)
>4: Set timezone to GMT! in: print.timeDate(list())
>  
>
>--------------------------------------------------------
>
>If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here for important additional terms relating to this e-mail.     http://www.ml.com/email_terms/
>--------------------------------------------------------
>
>  
>

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Date calculations

Mahesh Krishnan
In reply to this post by henaffp
Henaff,

I have found the following to be of use when using Date manipulations.

Example:

 tDate <- as.Date("03/22/2006", "%m/%d/%Y") ;

tDate +1    ## will give the update 03/23/2006 and so on..

Best regards,

Mahesh.

ps: fyi: the capital Y in as.Date refers to a 4 digit year representation


On 3/21/06, P. Hénaff <[hidden email]> wrote:

>
> I'm a rank beginner in R and R-metrics, but experienced in matlab and
> other languages. I'm strugling with date arithmetics in R. I'm doing
> statistical analysis on financial time series, using its, which uses POSIXct
> objects to measure time. What is the best way to perform simple date
> arithmetic with these objects? I do not care about h/m/s, nor do I care
> about daylight saving time. Example of a problem I faced:
>
> Add one day to 3-apr-2005. (daylight saving starts on 4-apr-2005).
> If I do
> > as.POSIXct("2005-04-03") + 86400
> I get 1 a.m. on the 4th of april.
>
> So, I do something like
> > dt1 <- as.POSIXct("2005-04-03")
> > x1 <- timeDate(dt1)
> > x2 <- timeNdayOnOrAfter(x1, n=2)
> > dt2 <- as.POSIXct(as.POSIXlt(x2))
>
> There has to be something simpler!
>
> Another problem. The tseries and timeDate packages seem to require that
> one changes his timezone environment variable to GMT. This is not really
> possible in a corporate environment. Is there a work-around?
>
> Any help will be much appreciated.
>
> Patrick Henaff
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
        [[alternative HTML version deleted]]


_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
Reply | Threaded
Open this post in threaded view
|

Re: Date calculations

Steve Miller-4
If you use the date or survival libraries, you can do something like:
as.date("01/10/2001", "mdy"), as well as as.date(14985) -- a julian
argument. You can also go back and forth between julians and numeric vectors
with mdy.date and date.mdy.

Steve Miller

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Mahesh
Krishnan
Sent: Wednesday, March 22, 2006 10:31 AM
To: [hidden email]
Subject: Re: [R-sig-finance] Date calculations

Henaff,

I have found the following to be of use when using Date manipulations.

Example:

 tDate <- as.Date("03/22/2006", "%m/%d/%Y") ;

tDate +1    ## will give the update 03/23/2006 and so on..

Best regards,

Mahesh.

ps: fyi: the capital Y in as.Date refers to a 4 digit year representation


On 3/21/06, P. Hinaff <[hidden email]> wrote:
>
> I'm a rank beginner in R and R-metrics, but experienced in matlab and
> other languages. I'm strugling with date arithmetics in R. I'm doing
> statistical analysis on financial time series, using its, which uses
POSIXct

> objects to measure time. What is the best way to perform simple date
> arithmetic with these objects? I do not care about h/m/s, nor do I care
> about daylight saving time. Example of a problem I faced:
>
> Add one day to 3-apr-2005. (daylight saving starts on 4-apr-2005).
> If I do
> > as.POSIXct("2005-04-03") + 86400
> I get 1 a.m. on the 4th of april.
>
> So, I do something like
> > dt1 <- as.POSIXct("2005-04-03")
> > x1 <- timeDate(dt1)
> > x2 <- timeNdayOnOrAfter(x1, n=2)
> > dt2 <- as.POSIXct(as.POSIXlt(x2))
>
> There has to be something simpler!
>
> Another problem. The tseries and timeDate packages seem to require that
> one changes his timezone environment variable to GMT. This is not really
> possible in a corporate environment. Is there a work-around?
>
> Any help will be much appreciated.
>
> Patrick Henaff
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance