Skip to content

formatting date strings

4 messages · Jeremy.Whish@csiro.au, Dirk Eddelbuettel, Jason Turner +1 more

#
Hi  all 

I am a relatively new R user so please excuse this question if it has been
covered some where else, just tell me where to find it. 

I have a simulation model that out puts dates in a standard dd/mm/yy format
R reads this as a factor and I cant find anything that will allow me to
convert them to a date. In S+ I have used a chron() function  that required
you to specify the format of the input date and the format of the output
date.
My question is: does R have such a function?  If not, can any one suggest a
way to tell R that information in dd/mm/yy format being read in with
read.table() is actually a date.

any help would be appreciated

Jeremy

Jeremy Whish, 
Farming Systems Research 
Agricultural Production Systems Research Unit, 
CSIRO Sustainable Ecosystems,
APSRU, PO Box 102, Toowoomba, Qld, Australia. 4350
Phone +61 (07) 4688 1419
Mobile 0428 763426
Fax  +61 (07) 4688 1193
Email jeremy.whish at csiro.au
WEB  http//www.apsru.gov.au 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Jeremy" == Jeremy Whish <Jeremy.Whish at csiro.au> writes:
  Jeremy> I have a simulation model that out puts dates in a standard
  Jeremy> dd/mm/yy format R reads this as a factor and I cant find anything
  Jeremy> that will allow me to convert them to a date. In S+ I have used a
  Jeremy> chron() function that required you to specify the format of the
  Jeremy> input date and the format of the output date.  My question is: does
  Jeremy> R have such a function?  If not, can any one suggest a way to tell
  Jeremy> R that information in dd/mm/yy format being read in with
  Jeremy> read.table() is actually a date.

R has a (ported) cron package which you install, but R also has the (more
powerful) DateTimeClasses. See the help pages for DateTimeClasses, as.POSIXct
and, in particular, strptime.

Here is a small example  [ note that I use %Y for yyyy, not %y for yy ]

First a data frame is created
`data.frame':   2 obs. of  2 variables:
 $ date  : Factor w/ 2 levels "10/1/2000","12/..": 1 2
 $ values: num  10 12

and the date information is parsed using strptime() with format information
[1] "2000-10-01" "2001-12-02"

which can then be reformatted, among other things. Here the format method of
the DateTimeClasses is used to impose the %Y%m%d format I prefer:
`data.frame':   2 obs. of  2 variables:
 $ date  : Factor w/ 2 levels "20001001","2001..": 1 2
 $ values: num  10 12


Dirk
#
On Wed, Feb 13, 2002 at 03:47:01PM +1100, Jeremy.Whish at csiro.au wrote:
...
Step 1 - you need to read it as plain dumb text first.
In help(read.table), check out the "as.is" option.  You probably want 
as.is=TRUE
Step 2 - you need to convert the plain dumb text to a date.

R has very sophisticated date/time handling - help(DateTimeClasses)
and help(strptime) will give you a starting background.  See if this
makes sense to you once you've read that.

my.data.frame$text.dates<-strptime(my.data.frame$text.dates,"%d/%m/%y")

Cheers

Jason
#
On Wed, 13 Feb 2002, Jason Turner wrote:

            
Please check out argument colClasses.  You want colClasses = "character'
for that column.

as.is is very confusing: it was for a long time incorrectly documented
which did not reduce the confusion.  I would like to deprecate it.