1
votes

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
----------       ----                        ---- 
1
I think this is rather a question for stack overflow (tag it with "R")alberto
You do not have Date-classed variables. You have factors most likely due to not using colClasses at the level of data input. This is a "frequently asked question". Read the FAQ about converting factors to character and then use as.Date with the correct format. There must be at least a hundred worked examples of how to do this on SO.IRTFM

1 Answers

0
votes

You can cheat a little,

You want to add -01-01 to the end of every date so that you can nicely convert it to a date.

data$Date = as.Date(paste0(data$Year,'-01-01'))

should work