[R] timeDate & business day

Tony Plate tplate at acm.org
Tue Mar 13 18:21:07 CET 2007


The R timeDate class is in the fCalendar package.

Does anyone know how to change the output format of a timeDate object? 
(Other than by explicitly supplying a format= argument to the format() 
function.)  I tried creating a timeDate object, and then changing the 
format slot.  However, all the functions I used on the object ('print', 
'format', 'as.character', 'show') seemed to ignore the value in the 
format slot.

And does anyone else find it a little confusing that print() and show() 
convert timeDate to the local time zone, but as.character() and format() 
display it in the time zone of its "FinCenter" slot?

Here is a transcript:

 > library(fCalendar)
 > tt <- c("2005-01-04", "2005-01-05", "2005-01-06", "2005-01-07")
 > x <- timeDate(tt)
 > x at format
[1] "%Y-%m-%d"
 > # Change the format on the timeDate object
 > x at format <- "%Y%m%d"
 > x
An object of class "timeDate"
Slot "Data":
[1] "2005-01-03 17:00:00 Mountain Standard Time"
[2] "2005-01-04 17:00:00 Mountain Standard Time"
[3] "2005-01-05 17:00:00 Mountain Standard Time"
[4] "2005-01-06 17:00:00 Mountain Standard Time"

Slot "Dim":
[1] 4

Slot "format":
[1] "%Y%m%d"

Slot "FinCenter":
[1] "GMT"

 > # Can get what I want by explicitly supplying format
 > # argument to format()
 > format(x, format="%Y%m%d")
[1] "20050104" "20050105" "20050106" "20050107"
 > # But format() seems to ignore the format slot
 > format(x)
[1] "2005-01-04" "2005-01-05" "2005-01-06" "2005-01-07"
 > print(x)
GMT
[1] [2005-01-04] [2005-01-05] [2005-01-06] [2005-01-07]
 > as.character(x)
[1] "2005-01-04" "2005-01-05" "2005-01-06" "2005-01-07"
attr(,"control")
FinCenter
     "GMT"
 >
 > sessionInfo()
R version 2.4.1 (2006-12-18)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"
[7] "base"

other attached packages:
   fCalendar     fEcofin
"240.10068" "240.10067"
 > Sys.getenv("TZ")
TZ
""
 >

-- Tony Plate

Michael Toews wrote:
> Those numbers look like ... well, numbers. You want characters! Try 
> converting the integer to a character before trying to do a string 
> parse, e.g.:
> 
> ymd.int <- c(20050104, 20050105, 20050106, 20050107, 20050110, 20050111, 
> 20050113, 20050114)
> ymd <- as.Date(as.character(ymd.int),"%Y%m%d")
> 
> As far as the other functions you are looking at ("timeDate", 
> "timeRelative") -- I've never seen these, so I'm guessing they are 
> S-PLUS. In R, you can use "diff" or "difftime" (which works with "Date" 
> and "POSIXlt"-or Date-Time classes) , e.g.:
> 
> diff(ymd)
> diff(ymd,2)
> diff(ymd,3)
> 
> or do some arithmetic:
> 
> difftime(ymd[1],ymd[4])
> difftime(ymd[1],ymd[4],unit="weeks")
> 
> Hopefully this is helpful to you!
> +mt
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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