I have two data frames with data from the same month, but on different days.
I want to match these to on a month by month basis, so "2017-07-01" and "2017-07-29" should both be seen as July 2017 when I combine these data frames.
They are dates, but I used the format(x, ...) function in order to remove the day from the Date columns.
This gives me the format "2017-07", but turns their class into characters,. When I use the as.Date(x, ...) function to assign a date to it, it removes are dates and gives me "NA" instead of "2017-07"
Does anyone know what I am doing wrong with the as.Date(), format() functions, or if if there is another way to match them by month?
A date object is always going to consist of three componenets, year, month and day. The usual way to collapse dates into months is to use the "date floor" (i.e. 2017-07-01)
library(lubridate)
floor_date(as.Date(c("2017-07-29", "2017-07-01")), unit = "month")
#> [1] "2017-07-01" "2017-07-01"
After you have made all your required calculations you can change the date format at the end as you have done in your example, but just for presentation purposes.