[R] Conversion from string to date type

Toby Gass tobygass at warnercnr.colostate.edu
Sun Mar 25 01:30:27 CET 2007


Hello, Andreas


I'm glad the previous replies helped you solve your problem.
I think your original problem, though,
was caused by a typo.  Note
the comma between the month and the year in your script and 
the
period between the month and the year in Jim Holtman's 
script.

>From Jim Holtman:

> Here is how you can convert them to a Date object:
>
>> x <- c('01.03.2007','02.03.2007','03.03.2007')
>> y <- as.Date(x, format="%d.%m.%Y")
>> y

>From Andreas Tillea:
>Well, this is what I tried when reading the docs, but

> mydata <- read.csv(file='mydata.dat', sep = '\t', 
> quote='', fill=TRUE,header=TRUE )
> datum <- as.Date(mydata["date"], "%d.%m,%y")
>Error in as.Date.default(mydata["date"], "%d.%m,%y") :
         do not know how to convert 'mydata["date"]' to 
class "Date"

>I also tried:

> datum <- strptime(imydata["date"], "%d.%m,%y")
> datum
>[1] NA
Hope this helps for the future

Toby

Toby Gass
Department of Forest, Rangeland, and Watershed Stewardship
Warner College of Natural Resources
Colorado State University
Fort Collins, CO  80523



More information about the R-help mailing list