[R] How can I read date format '02-Jan-02' ?

Jim Lemon jim at bitwrit.com.au
Mon Aug 16 12:30:22 CEST 2010


On 08/16/2010 08:22 PM, Jim Lemon wrote:
> On 08/16/2010 03:27 PM, Dong-Hee Koh wrote:
>> Hello,
>>
>> I am trying to read a database exported from SAS.
>>
>> It is form of csv, and the date format reads '02-Feb-99'.
>> I used following code to convert character to date format,
>> db$dob<-as.Date(db$dob, format="%d-%b-%y").
>> but it doesn't work, only seems NA.
>>
>> What's wrong with this code?
>
> Hi Dong-Hee,
> The problem is not with your command, but most likely the reading in of
> the data. If you just use read.csv, your dates will probably be read in
> as factors. You can correct this at the reading stage:
>
> db<-read.csv(<filename>,as.is=TRUE)
>
> or at the conversion stage:
>
> db$dob<-as.Date(as.character(db$dob,"%d-%b-%y"))
>
Oops, sorry, misplaced parenthesis

db$dob<-as.Date(as.character(db$dob),"%d-%b-%y")

Jim



More information about the R-help mailing list