[R] Error as.Date on Invalid Dates

jim holtman jholtman at gmail.com
Wed Jan 21 18:07:51 CET 2009


?try

On Wed, Jan 21, 2009 at 10:55 AM, Brigid Mooney <bkmooney at gmail.com> wrote:
> Hi All,
>
> I have an script in R which accepts user inputs for certain parameters,
> particularly dates, which the user inputs as character strings.
> eg:
>  > date1 <- "2009-01-21"
>
> The script later parses the input via the as.Date function:
>> as.Date(date1)
>
> However, as.Date encounters an error when the string does not represent an
> actual date.
> eg:
>  > date1 <- "2009-02-29"  # Note: 2009 not a leap year
>> as.Date(date1)
> Error in fromchar(x) :
>  character string is not in a standard unambiguous format
>
> As I have many instances of date entries like this, date1, date2, date3,
> etc. , I'd like the script to error out gracefully and to be able to point
> the user to which date they need to correct, rather than "Error in
> fromchar(x)...", which doesn't make it obvious what they need to do to fix
> the error.
>
> Ideally I'd love to send the user a message like:
> print(paste(date1, "is an invalid date.  Refer to calendar.", sep=" "))
>
> If anyone has any suggestions on catching this type of error and feedback
> which directs the user, it would be much appreciated.
>
> For reference, I am using a Windows Vista machine with:
>> R.version.string
> [1] "R version 2.8.0 (2008-10-20)"
>
> Thanks,
> Brigid
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list