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(
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(
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