[R] Feature request for as.Date() function

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 27 02:44:20 CET 2009


The situation he is referring to seems to be this:

> L <-  "date,value\n'2009-01-01',10\n'2009-02-01',1\n'NA', 3"
> read.csv(textConnection(L), colClasses = c("Date", "numeric"))
Error in charToDate(x) :
  character string is not in a standard unambiguous format

where all the fields in the date column are quoted.

On Wed, Nov 25, 2009 at 2:56 PM, jim holtman <jholtman at gmail.com> wrote:
> 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!




More information about the R-help mailing list