-5

Possible Duplicate:
how to convert y%m%d%H format into “%Y%m%d %H:%M:%S” in time series data

How to convert the date format is %y%m%d(000101=20000101) to %Y%m%d(20000101) in R dataframes(

Community
  • 1
  • 1
user1537175
  • 95
  • 1
  • 3
  • 6
  • In the future it would be nice if a clearer example was included, but you did include the starting values (000101) and what the results should look like. Thanks for that :) – Joshua Jul 23 '12 at 15:25
  • Not quite a duplicate I think -- just closely related. – Ben Bolker Jul 23 '12 at 15:25
  • 1
    @BenBolker. IMHO it's close enough to a dupe, that the OP should have edited the other question instead of creating a new one. -1 – GSee Jul 23 '12 at 15:28
  • 4
    Hi! Welcome to StackOverflow! To avoid negative reactions like these to any future questions you may ask, I strongly recommend you read [this](http://stackoverflow.com/q/5963269/324364) question on how to write a great question about R and make sure to follow its advice in the future. – joran Jul 23 '12 at 15:29
  • @GSee yeah they are pretty darn close. – Joshua Jul 23 '12 at 15:29

1 Answers1

6

It is fairly easy. I am going to assume that currently, your dates are stored as strings, as in the variable x.

x <- "000101"

First you can convert this to a date class. Once that is done, R stores the dates as days since the origin. Meaning that it is really numeric underneath. However, special printing and display methods, show a prettier output when printed than the underlying numbers. Converting to a date and forcing printing by adding ():

(xd <- as.Date(x, format = "%y%m%d"))
[1] "2000-01-01"

Now if you want to display it some non-default way (such as %Y%m%d), you can just ask R to format it that way.

format(xd, "%Y%m%d")
[1] "20000101"

and if you wanted, you could save the results back into your data frame.

(myx <- format(xd, "%Y%m%d"))
[1] "20000101"

Note that the results will again be character (string) class, not a date class. That means (among other things) that you can no longer add or subtract, etc. For example:

xd + 10
[1] "2000-01-11"
xd - 10
[1] "1999-12-22"

but

myx - 10
Error in myx - 10 : non-numeric argument to binary operator
Joshua
  • 686
  • 3
  • 7
  • @BenBolker edited to show an example of what I meant. Sorry for not being clearer. – Joshua Jul 23 '12 at 15:28
  • I was just giving you a hard time about spelling. ("substract" for "subtract" is a very common mistake among non-native English speakers -- have never paid enough attention to know if it's specific to particular languages) – Ben Bolker Jul 23 '12 at 15:45
  • Whoops. Fixed now. A bit embarrassed to say I am a native English speaker (of some sort). – Joshua Jul 23 '12 at 15:48
  • 1
    I figured. Anyway, I was just teasing (but a bit too telegraphically). – Ben Bolker Jul 23 '12 at 15:49
  • subtract: from latin ``sub-traere``, so the s should not be there. Sadly it is there in French and Spanish (but not in Italian or Portuguese). So annoying. – PatrickT Nov 30 '16 at 17:08