With monthly measurements points

I have to visualize data with daily and with monthly measurements points.
I can deal well with the daily data:
data_daily$day <- as.Date(data_daily$day, "%Y-%m-%d")

But as.Date(data_monthly$month, "%Y-%m") seems not to be meaningful for data_monthly$month e.g. like '2017-11', '2017-11', '2017-11', '2017-12', '2017-12', '2017-12', '2018-01'...
After:

data_monthly$month <- as.Date(data_monthly$month, "%Y-%m") 

data_monthly$month become 'N/A' ...
I explicitly transformed the data in my csv file before reading into R like: '2017-11-01', '2017-11-01', '2017-11-01', '2017-12-01', '2017-12-01', '2017-12-01', '2018-01-01'... and it seems to be a sort of solution, but I am not quite happy.

Thank you for advices.

BR, P.

Hi @pep, welcome to community!

reprex - Firstly, just a note that it would be awesome if you could rewrite your post with a more reproducible example -- reprex. Here's some advice on what that means FAQ: What's a reproducible example (`reprex`) and how do I create one?.

What happening? - Here's a nice explanation of what's going on here (gist, a Date in R is the number of days since some origin date, usually the epoch - so you need a day in addition to year and month with as.Date()):

Here appears to be a tidyverse solution:

As @EconomiCurtis said you should provide a reprex. The reprex in CRAN right now has some issues so my will find it easier to use the version in github ( which will eventually end up in CRAN).

Until CRAN catches up with the latest version install reprex with

devtools::install_github("tidyverse/reprex")

Also you reprex should be as simple as possible and illustrate the particular issue you are running into rather than the overall code that that is running into the issue.

In any case the problem you are probably running into is that as.Date creates a Date object and a Date object contains the number of days from 1970-01-01. Since something like "2017-05" doesn't specify a particular day there is no way it can compute the number of days form 1970-01-01.

If you want to use as.Date as a year + month just make a convention that year-month always specify the first day of the month.

You could make a helper function to make it more convenient to implement this year-date conventions, like this:


makeMonthDate <-function(year_month) {
   usedate <- paste(year_month, "-01", sep="")
   as.Date(usedate, "%Y-%m-%d")
}

makeMonthDate("2017-4")
#> [1] "2017-04-01"

Created on 2018-03-05 by the reprex package (v0.2.0).

2 Likes

Thank you for the replays and advices.
@danr Thank you also for solution with the makeMonthDate function.

So, if I understood correctly, I was on the right way (adding the first of the month to each month: yyyy-mm -> Date object) and there is no month object or sort of.

I need this for a visualization (plot, ggplot but also rCharts nvd3). I have some difficulties. I would post separate topic for visualization of a Date with a monthly measurements points using the reprex package.