I have a dataset that looks like this:
Month count
2009-01 12
2009-02 310
2009-03 2379
2009-04 234
2009-05 14
2009-08 1
2009-09 34
2009-10 2386
I want to plot the data (months as x values and counts as y values). Since there are gaps in the data, I want to convert the Information for the Month into a date. I tried:
as.Date("2009-03", "%Y-%m")
But it did not work. Whats wrong? It seems that as.Date() requires also a day and is not able to set a standard value for the day? Which function solves my problem?
I think @ben-rollert's solution is a good solution.
You just have to be careful if you want to use this solution in a function inside a new package.
When developping packages, it's recommended to use the syntaxe
packagename::function_name()
(see http://kbroman.org/pkg_primer/pages/depends.html).In this case, you have to use the version of
as.Date()
defined by thezoo
library.Here is an example :
So if you're developping a package, the good practice is to use :
You could also achieve this with the
parse_date_time
orfast_strptime
functions from thelubridate
-package:The difference between those two is that
parse_date_time
allows for lubridate-style format specification, whilefast_strptime
requires the same format specification asstrptime
.For specifying the timezone, you can use the
tz
-parameter:When you have irregularities in your date-time data, you can use the
truncated
-parameter to specify how many irregularities are allowed:Used data:
Using anytime package:
Since dates correspond to a numeric value and a starting date, you indeed need the day. If you really need your data to be in Date format, you can just fix the day to the first of each month manually by pasting it to the date:
Try this. (Here we use
text=Lines
to keep the example self contained but in reality we would replace it with the file name.)The X axis is not so pretty with this data but if you have more data in reality it might be ok or you can use the code for a fancy X axis shown in the examples section of
?plot.zoo
.The zoo series,
z
, that is created above has a"yearmon"
time index and looks like this:"yearmon"
can be used alone as well:Note:
"yearmon"
class objects sort in calendar order.This will plot the monthly points at equally spaced intervals which is likely what is wanted; however, if it were desired to plot the points at unequally spaced intervals spaced in proportion to the number of days in each month then convert the index of
z
to"Date"
class:time(z) <- as.Date(time(z))
.The most concise solution if you need the dates to be in Date format:
as.Date
will fix the first day of each month to a yearmon object for you.