a.Date() function creates "NA NA NA NA NA NA"

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?

Thanks!

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"

Created on 2020-08-14 by the reprex package (v0.3.0)

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.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.