convert 'Year' to Year-Month-Day format

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

convert 'Year' to Year-Month-Day format

kuma
This post has NOT been accepted by the mailing list yet.
I have a datafile in the following format:
Year      Observed Flow        Simulated Flow
2000       210                          256
2001       560                          568
2002       435                          896
2003       58                           95
----        ----                        ----

Using plot2(...)gives the graph of observed and simulated flows in Y axis and Year in x axis (one graph). However, all Year format did not appear in the graph, rather it appeared as 1, 2, 3...etc. But I want to see the x-axis label in actual Year format like 2000, 2001, 2002, 2003...etc.

I tried using as.Date(...) and some other commands like as.POSIXct, however, couldn't able to convert 'Year' to "Year-Month-Day" format. The idea of converting Year into Year-Month-Day format is to read the data using read.zoo command, and thus, utilize plot2(....).

So the question is: How can I convert "Year" column to "Year-Month-Day" column so that the data would look like this:

Date             Observed Flow        Simulated Flow
2000-01-01       210                          256
2001-01-01       560                          568
2002-01-01       435                          896
2003-01-01       58                           95
----------         ----                        ----

====================
Ramesh Gautam, PhD, PE
Department of Water Resources
Sacramento, CA USA
rgautam@water.ca.gov
Ph: 916-574-2638
====================
Reply | Threaded
Open this post in threaded view
|

Re: convert 'Year' to Year-Month-Day format

colleca
This post has NOT been accepted by the mailing list yet.
Try this, I encountered a similar problem

fixed<-paste(DF$Year,"01-01",sep="") #DF is your data frame
DF$Year<-as.Date(fixed,format="%Y-%m-%d")

You should now be able to manipulate it to your hearts content.

Very helpful website for dealing with dates:
http://www.stat.berkeley.edu/~s133/dates.html

Best,
Tony