Unable to format months with as.Date
The simplest answer is that a date is something which includes a day and if one is not specified, as.Date() gets confused. From the ?as.Date documentation:
If the date string does not specify the date completely, the returned answer may be system-specific. The most common behaviour is to assume that a missing year, month or day is the current one. If it specifies a date incorrectly, reliable implementations will give an error and the date is reported as ‘NA’. Unfortunately some common implementations (such as ‘glibc’) are unreliable and guess at the intended meaning.
When you think about it, a term such as "Mar/1947" is not, strictly speaking, a date - it's just a combination of month and year. A date is a specific day in March 1947 (or any other month + year) - since you don't specify one, you don't have a date.
It is because d2
in your data.frame
is a malformed date. It doesn't contain a day. To get round this, consider using the following:
d1$date2 <- as.Date(x=paste("1/",d1$d2, sep=""), format="%d/%b/%Y")
> d1
d d2 date1 date2
1 1/Jan/1947 Jan/1947 1947-01-01 1947-01-01
2 1/Feb/1947 Feb/1947 1947-02-01 1947-02-01
3 1/Mar/1947 Mar/1947 1947-03-01 1947-03-01