[R] Check Dates

David Winsemius dwinsemius at comcast.net
Sat Jul 10 16:10:29 CEST 2010


On Jul 10, 2010, at 9:29 AM, Wang, Kevin (SYD) wrote:

> Hi,
>
> I'm trying to check a column of dates and creating indicator  
> variables showing different time periods.
>
> For example, if I were to do this in Excel I would have something  
> like:
>   Year to Dec 2004=IF(AND(referral date<DATE(2005,1,1), OR($closure  
> date>DATE(2003,12,31), LEN($closure date) = 0)), "Yes", "No")
> for the "Year to Dec 2004" column.
>
> I've read the data into R and converted the two columns (referral  
> date and closure date) to as.Date() format (found this format from r- 
> help):
> referral <- as.Date(issues.sub$Referraldate, "%d/%m/%Y")
> closure <- as.Date(issues.sub$Dateofclosure, "%d/%m/%Y")
>
> But then have trouble doing the "if" checking.  I tried something  
> like:
> Dec04<- ifelse(referral < as.Date("1/1/2005", "%d/%m/%Y") &
>               (closure > as.Date("31/12/2003", "%d/%m/%Y") ||  
> closure = ""), "Yes", "No")
>
> but got an error message:
> Error in closure > as.Date("31/12/2003", "%d/%m/%Y") || closure = "" :
>  could not find function "||<-"

Seems like a pretty informative error message. With ifelse you need to  
be using the vectorized logical operators, "&", "|", "==", and "!".   
"||" is for use with the "if (.) {.} else{.}" construct. Of course,  
when you solve the "||" error, it's likely that your improper use of  
"=" will sneak up and throw a different error message.

>
> I haven't really played with dates in R before so would really  
> appreciate some guidance on this.

Cannot tell if it's due to your Date formatted fields since you have  
not offered str on any of your variables.

>
> (I'm avoiding Excel because there are over 130,000 rows to check,  
> and I need to repeat this process for each quarter since Dec 2004.   
> When I tried this in Excel it just took all the memory I have on my  
> computer and freezes the entire workbook).

It would probably be faster to define your date fences outside the  
ifelse function, then they do not need to be recalculated at every row  
of the data.

>
> Cheers
> Kevin
>
>
> Kevin Wang
> Senior Advisor, Health and Human Services Practice
> Government Advisory Services
>
> KPMG
> 10 Shelley Street
> Sydney  NSW  2000  Australia
>
> Tel     +61 2 9335 8282
> Fax     +61 2 9335 7001
>
> kevinwang at kpmg.com.au
>
>
>
>
>
> ******************************************************************
>
> The information in this e-mail is confidential and may be legally
> privileged. It is intended solely for the addressee. Access to this
> e-mail by anyone else is unauthorised. If you have received this
> communication in error, please notify us immediately by return
> e-mail with the subject heading "Received in error" or telephone
> +61 2 93357000, then delete the email and destroy any copies of it.
> If you are not the intended recipient, any disclosure, copying,
> distribution or any action taken or omitted to be taken in reliance
> on it, is prohibited and may be unlawful. Any opinions or advice
> contained in this e-mail are subject to the terms and conditions
> expressed in the governing KPMG client engagement letter. Opinions,
> conclusions and other information in this e-mail and any
> attachments that do not relate to the official business of the firm
> are neither given nor endorsed by it.
>
> KPMG cannot guarantee that e-mail communications are secure or
> error-free, as information could be intercepted, corrupted,
> amended, lost, destroyed, arrive late or incomplete, or contain
> viruses.
>
> KPMG, an Australian partnership and a member firm of the KPMG
> network of independent member firms affiliated with KPMG
> International Cooperative (“KPMG International”), a Swiss entity.
> KPMG International provides no services to clients.
>
> Liability limited by a scheme approved under Professional Standards
> Legislation.
>
> *
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list