[R] Calculating the trading days

Girish A.R. garamach at gmail.com
Fri Jul 17 08:18:45 CEST 2009


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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Calculating-the-trading-days-tp24528798p24529159.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list