[R] Calculating the trading days

spencerg spencer.graves at prodsyse.com
Sat Jul 18 21:31:25 CEST 2009


      You also need to know which days are trading vs. non-trading 
days.  To find functions to provide this additional information, I first 
tried the following: 


library(RSiteSearch)
td <- RSiteSearch.function('trading days')
HTML(td)


      This produced nothing.  Therefore, I divided the task up as follows: 


dow <- RSiteSearch.function('day of week')
hol <- RSiteSearch.function('holiday')
hols <- RSiteSearch.function('holidays')
td. <- dow|hol|hols
HTML(td.)

   
      This identified 114 help pages in 45 packages matching at least 
one of these search strings.  The first 13 were in package "fCalendar", 
followed by the same 13 in "timeDate", which is the more recent version 
of "fCalendar".  This included functions called "dayOfWeek", "holiday", 
"holidayZURICH", "holidayNYSE", etc.  If you want trading days on the 
NYSE or the financial markets in Zurich, you should be able to translate 
this into code to do what you want. 


       Hope this helps. 
      Spencer Graves


Girish A.R. wrote:
> Hi Ravi,
>
> See if the following helps:
> =========================
> Lines <- "Date1 Date2 
> 29-Dec-06 25-Jan-07
> 29-Dec-06 25-Jan-07
> 29-Dec-06 25-Jan-07
> 2-Jan-07 25-Jan-07
> 2-Jan-07 25-Jan-07
> 2-Jan-07 25-Jan-07"
> DF <- read.table(con<- textConnection(Lines), skip = 1)
> close(con); 
> names(DF) <- scan(textConnection(Lines), what = "", nlines = 1) 
> DF$D1 <- as.Date(DF$Date1,"%d-%b-%y")
> DF$D2 <- as.Date(DF$Date2,"%d-%b-%y")
> diff <- difftime(DF$D2,DF$D1,units="days")
>
>   
>> diff
>>     
> Time differences in days
> [1] 27 27 27 23 23 23
>
> =============================
>
> cheers,
> -Girish
>
>
> Ravi S. Shankar wrote:
>   
>> Hi R,
>>
>> I have two columns of date in a CSV file in the below format
>> 29-Dec-06	25-Jan-07
>> 29-Dec-06	25-Jan-07
>> 29-Dec-06	25-Jan-07
>> 2-Jan-07	25-Jan-07
>> 2-Jan-07	25-Jan-07
>> 2-Jan-07	25-Jan-07
>>
>> I read in R using   dat<-read.csv("Z:\\data.csv"). 
>>     
>>> class(dat[,1])
>>>       
>> [1] "factor"
>>
>> So I use
>> dat[,1]=as.Date(as.character(dat[,1]),"%d-%b-%YY")
>>     
>>> class(dat[,1])
>>>       
>> [1] "Date"
>>  But when I do 
>>     
>>> fix(dat)
>>>       
>> Warning: class discarded from column 'Date'
>> Also 
>>     
>>> dat[1,1]
>>>       
>> [1] NA
>>
>> My task is to compute the number of trading days between the two dates
>> in col A and Col B. Any help would be appreciated!
>>
>> Thank you,
>> Ravi Shankar S 
>> This e-mail may contain confidential and/or privileged i...{{dropped:10}}
>>
>> ______________________________________________
>> 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