nabble.30.miller_2555 at spamgourmet.com
2009-Nov-25 17:38 UTC
[R] Feature request for as.Date() function
Hello - I have a csv file with a few date columns. Some of the records have an "NA" character string instead of the date. When I attempt to use read.csv() and typecast the columns using colClasses, I receive the following error: Error in charToDate(x) : character string is not in a standard unambiguous format Similarly, the following command produces the same error: as.Date("NA") However, as.Date(NA) performs as documented. Can we enhance the as.Date() function to convert "NA" strings into NA value prior to type conversion? Thanks!
Seems to work fine in my testing:> x <- read.csv(textConnection("date,value+ 2009-01-01,10 + 2009-02-01,1 + 'NA', 3"), colClasses=c("Date", 'integer'))> > str(x)'data.frame': 3 obs. of 2 variables: $ date :Class 'Date' num [1:3] 14245 14276 NA $ value: int 10 1 3> x <- read.csv(textConnection("date,value+ 2009-01-01,10 + 2009-02-01,1 + NA, 3"), colClasses=c("Date", 'integer'))> > str(x)'data.frame': 3 obs. of 2 variables: $ date :Class 'Date' num [1:3] 14245 14276 NA $ value: int 10 1 3>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. On Wed, Nov 25, 2009 at 12:38 PM, <nabble.30.miller_2555 at spamgourmet.com> wrote:> Hello - > > I have a csv file with a few date columns. Some of the records have an > "NA" character string instead of the date. When I attempt to use > read.csv() and typecast the columns using colClasses, I receive the > following error: > ? ?Error in charToDate(x) : > ? ? ?character string is not in a standard unambiguous format > > Similarly, the following command produces the same error: > ? ?as.Date("NA") > > However, as.Date(NA) performs as documented. > > Can we enhance the as.Date() function to convert "NA" strings into NA > value prior to type conversion? > > Thanks! > > ______________________________________________ > 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?
nabble.30.miller_2555 at spamgourmet.com
2009-Nov-25 21:40 UTC
[R] Feature request for as.Date() function 20)
On Wed, Nov 25, 2009 at 2:56 PM, jim holtman - jholtman at gmail.com <+nabble+miller_2555+9dc9649aca.jholtman#gmail.com at spamgourmet.com> wrote:> Seems to work fine in my testing: > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >> >> Similarly, the following command produces the same error: >> ? ?as.Date("NA") >> >> However, as.Date(NA) performs as documented. >> >> Can we enhance the as.Date() function to convert "NA" strings into NA >> value prior to type conversion?I sincerely appreciate the help, but with all due respect, I have read the posting guide and did provide the minimal code necessary to reproduce the desired feature. To reiterate, I would like to be able to feed the character string "NA" to the as.Date() function to yield the same result as `as.Date(NA)`. Please advise if testing the following does not yield an error:> as.Date("NA");This may or may not aid the read.csv() error message in my particular code (for which a workaround has already been identified). Thank you.
nabble.30.miller_2555 at spamgourmet.com
2009-Nov-26 19:01 UTC
[R] Feature request for as.Date() function
On Thu, Nov 26, 2009 at 12:08 AM, jim holtman - jholtman at gmail.com <+nabble+miller_2555+9dc9649aca.jholtman#gmail.com at spamgourmet.com> wrote:> An easy way is just to write your own function that will accept "NA", > convert it to NA and then call as.Date. >I have written such a function, which has provided the temporary workaround mentioned. ( I am not that lazy yet :-) )> R is a functional language, so write some functions. ?Don't try to > overload existing functions with new options that may break a lot of > existing code. ?If you have special requirements, then adapt your code > to them. ?You would probably have to wait around for a long time > before an new option got in, so it is easier to create your own.I do not mind waiting for the additional functionality (and it is no longer an immediate need given the workaround). I was attempting to contribute to the continued enhancement of an open source project. Since the as.Date() function already defines "standard unambiguous formats," and since "NA" (and "NaN", "Inf", etc) are not ambiguous within the transform to their numeric counterparts, it stands to reason that this is logical behaviour of this function. I also doubt this enhancement would break moderate-to-well-designed code since: (1) Existing code would enact a stop() condition based on the current implementation, forcing error-handling, if any. (2) Converting "NA" (and "NaN", "inf", etc) is not ambiguous. Coders feeding such strings should expect their numeric counterparts. In all likelihood, coders would convert these strings manually in error-handling code anyway. I have my solution, but wanted to better the project for use by other community members. The R Core Development Team is welcome to accept or ignore the suggestion. I do appreciate the time to discuss this topic, but will consider the matter closed for my part. Thanks.