Character conversion to date using lubridate

I rather new to R, so please excuse any errors. I've only posted a few times.

I have large dataset in tidy format. The Month column in the dateframe is in character format. I am trying to convert the Month column to a date format for each month so I can graph the timeseries without having to scale the x-axis using discrete limits. The simple example is as follows:

library(tidyverse)
library(lubridate)

# character string as months
Month=c("January", "February", "March")
# convert character to date format
Month <- Month %>% as_date(Month)

But I receive the following error:
Warning message: All formats failed to parse. No formats found.

Is there a way to convert the character to date?

Thanks

Perhaps there is a cleaner way to do this, but I would use the following.

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
# character string as months
Month=c("January", "February", "March")
# convert character to date format
Month <- as_date(paste0("2019-", Month, "-01"))
class(Month)
#> [1] "Date"
Month
#> [1] "2019-01-01" "2019-02-01" "2019-03-01"

Created on 2019-04-23 by the reprex package (v0.2.1)

Month is not enough to identify a date. One way to do it is to use something like @FJCC suggested. Another is to use something like tsibble::yearmonth(). In absence of year, it'll default to year 1400:

tsibble::yearmonth(c("January", "February", "March"))
#> [1] "1400 Jan" "1400 Feb" "1400 Mar"

Created on 2019-04-24 by the reprex package (v0.2.1)

This was helpful. Thank-you.

Thank-you for the feedback. It worked with the code you posted.

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.