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.
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"
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: