[R] How canI convert date-time to Julian date?

Gabor Grothendieck ggrothendieck at myway.com
Fri Apr 2 18:23:58 CEST 2004



Convert your character dates to one of R's date classes
(POSIXlt, chron or the new Date class in 1.9.0) and then
format the date.  The m/d/y format you want is actually the
default format in chron:

z <- c( "2004-01-20", "2004-02-22" )  # test vector

# load chron and set default year to 4 digits
require(chron)
options( chron.year.abb = FALSE ) 

# convert date to chron and format
format( chron(z, format = "y-m-d", out.format = NULL) )

The options statement makes chron use 4 digit years by
default and the out.format=NULL tells chron to use its
default format for output rather than the format that
the dates were input in.

One caveat.  Ensure that you really do have character dates.
If they were read in using read.table they may be factors.
If that's the case convert them to character first:

z <- as.character(z)


Martina Azaroglu <mazaroglu <at> gmx.de> writes:

: 
: Hello!!
: I need some help! I tried everything, but nothing worked!
: I have a vector c with dates in it, in the format "2004-04-01" and i need to
: convert it in the form 
: "04/01/2004" or "01/04/2004" !
: How can i do that??
: 
: Thanks
:  Bye Martina
: 
: --
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
: 
:




More information about the R-help mailing list