The OP has requested to convert Year-Mon (without day of the month) to Date from a data.frame column which is a factor. Without day of the month, the date is incomplete which produces NA
s.
There are various options available to deal with incomplete dates.
as.Date()
with day of the month supplemented
As suggested in a similar form by d.b:
as.Date(paste0(hand$date, "-01"), "%y-%b-%d")
#[1] "2014-01-01" "2014-02-01" "2014-03-01" "2015-01-01"
lubridate::ymd()
The ymd()
function of the lubridate
package has a truncated
parameter to parse incomplete dates:
lubridate::ymd(hand$date, truncated = 1L)
#[1] "2014-01-01" "2014-02-01" "2014-03-01" "2015-01-01"
Note that lubridate
automatically has assumed the first day of each month.
zoo::as.yearmon()
and zoo::as.Date()
The option to use the as.yearmon()
function from the zoo
package has already been suggested by Sagar and statoptim.
The answer of Sagan is incomplete because as.yearmon()
returns an object of class yearmon
but not Date
:
str(zoo::as.yearmon(hand$date, "%y-%b"))
#Class 'yearmon' num [1:4] 2014 2014 2014 2015
The answer of statoptim is unnecessarily complicated as yearmon
can directly be coerced to Date
:
zoo::as.Date(zoo::as.yearmon(hand$date, "%y-%b"))
#[1] "2014-01-01" "2014-02-01" "2014-03-01" "2015-01-01"
Note that we have to use zoo::as.Date()
if we haven't loaded zoo
beforehand because base R's as.Date()
doesn't know how to handle yearmon
objects.
zoo::as.Date()
has automatically assumed the first day of each month by default. The frac
parameter can be used to control which day of the month is returned, e.g.,
zoo::as.Date(zoo::as.yearmon(hand$date, "%y-%b"), frac = 1)
#[1] "2014-01-31" "2014-02-28" "2014-03-31" "2015-01-31"
returns the last day of each month.
Caveat
It might be the case that the current locale may influence the interpretation of the abbreviated month names (which might be the case in statoptim's answer).
There's an answer to a related question which suggests to check out the examples section of ?as.Date
:
## read in date info in format 'ddmmmyyyy'
## This will give NA(s) in some locales; setting the C locale
## as in the commented lines will overcome this on most systems.
## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- as.Date(x, "%d%b%Y")
## Sys.setlocale("LC_TIME", lct)
z