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 --
On Fri, 2 Apr 2004, Martina Azaroglu wrote:> I need some help! I tried everything, but nothing worked!I am sure you did not try _everything_, but it would have been helpful to give some idea of what you were trying.> 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" !In 1.9.0 beta you can do format(as.Date("2004-04-01"), "%m/%d/%Y") if you mean American usage for dates. But that has nothing to do with 'Julian date'. In earlier versions format(as.POSIXlt("2004-04-01"), "%m/%d/%Y") will do this too.> How can i do that??Try help.search? For example, help.search("date") in 1.8.1 gave as.POSIXct(base) Date-time Conversion Functions format.POSIXct(base) Date-time Conversion Functions to and from Character which are the correct pages for my solution above. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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 : :