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]]
?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?
-----begin included message -------- 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. ------- end inclusion --------- One idea is to use the as.date function, for the older (and less capable) 'date' class. This is currently loaded by default with library(survival). It returns NA for an invalid date rather than dying.> as.date(c("2009-5-10", "2007/2/29", "1953/3/10"), order='ymd')[1] 10May2009 NA 10Mar53 The order argument in needed here since the default assumption is the US habit of month-day-year. You can then convert to the more modern format.> temp <- as.date(c("2009-5-10", "2007/2/29", "1953/3/10"), order='ymd') > as.Date(temp)[1] "2009-05-10" NA "1953-03-10" Terry Therneau Note: as.Date will return a string with NA's as well, AS LONG AS the first date in the sequence is legal. It uses the first to pick a format (I presume).
I am relatively new to R, so maybe I am miss something, but I now tried the as.Date now and have problems understanding how it works (or don't work as it seem). Brian D Ripley wrote:> On Thu, 22 Jan 2009, Terry Therneau wrote: >> >> One idea is to use the as.date function, for the older (and less capable) 'date' >> class. This is currently loaded by default with library(survival). It returns >> NA for an invalid date rather than dying. > > So does as.Date **if you specify the format** (as you have to with your as.date: > it has a default one):as.Date("2001/1/1") Works fine as.Date("1/1/2001") Prints "1-01-20" ??? as.Date("13/1/2001") Prints "13-01-20" ??? as.Date("1/13/2001") Prints error: not in standard unambigous format It seems that as if both "1/1/2001" and "13/1/2001" were considered by R to be in a standard unambiguous format (or otherwise an error be reported?) and yet they are parsed incorrectly according to what one could think is obvious. It is also surprizing that not only "13/1/2001" but also "1/2/2001" and "2/1/2001" are successful but incorrect parsed as if they are unambiguous, and yet "13/1/2001" is ambiguous, though there is really just one way to parse it meaningfully. I think the strings that are incorrectly parsed should raise errors, and the last example should be succesful parsed. What is the reason for the observed? Mvh. Marie [[alternative HTML version deleted]]