If you really want to convert it to the "Date" class using the first month, then Roland's solution seems the most straightforward, but there are other considerations, for example, whether you can use the end of the month or really want to represent year-months using dates, first .
The zoo package has a class "yearmon" that can represent year-months directly without converting them to dates, and also has an as.Date.yearmon method, which has a frac= argument to indicate the fraction of the month path for conversion, if you want a class "Date" .
First make sure dates are character strings. The input in the question shows 1993.10 as one of the input, so we need to make sure that there is a finite zero. (If the inputs are already characters with a trailing zero, this is not a problem. We assumed that the worst case here is a numeric value, so we need to explicitly convert them to characteristic bites with a trailing 0 if necessary.) Now use as.yearmon with format "%Y.%m" . Finally, use as.Date.yearmon to convert to the "Date" class.
Perhaps the biggest advantage of this approach is that we could just leave the result in the "yearmon" class (that is, omit the "as.Date" part, for example as.yearmon(sprintf("%.2f", dates)) ), or if the dates were already character strings, dates.ch , with a trailing 0 in the case of "1993.10" , and then just as.yearmon(dates.ch, "%Y.%m") , which really represents what you’re better, because the day doesn’t really matter, given that it wasn’t in the beginning. "yearmon" objects can be built and sorted in the expected way.
The following is a conversion to the "Date" class using "yearmon" :
library(zoo) dates <- c(1993.07, 1993.08, 1993.09, 1993.1, 1993.11, 1993.12) # test input as.Date(as.yearmon(sprintf("%.2f", dates), "%Y.%m")) # 1st of month ## [1] "1993-07-01" "1993-08-01" "1993-09-01" "1993-10-01" "1993-11-01" "1993-12-01" as.Date(as.yearmon(sprintf("%.2f", dates), "%Y.%m"), frac = 1) # last of month ## [1] "1993-07-31" "1993-08-31" "1993-09-30" "1993-10-31" "1993-11-30" "1993-12-31"
or if the test input looks like this:
dates.ch <- c("1993.07", "1993.08", "1993.09", "1993.10", "1993.11", "1993.12") # input as.Date(as.yearmon(dates.ch, "%Y.%m")) as.Date(as.yearmon(dates.ch, "%Y.%m"), frac = 1)