I have a string variable that I want to parse to class Date
. In addition to the day, year and month, the format has other characters like separators (,
You don't need to strip the characters not used in the conversion specification. In the Details section of ?strptime
we find that:
"[a]ny character in the format string not part of a conversion specification is interpreted literally"
That is, in the format
argument of as.Date
, you may include not only the conversion specification (introduced by %
) but also the "other characters":
Furthermore, from ?as.Date
:
Character strings are processed as far as necessary for the format specified: any trailing characters are ignored
Thus, this works:
as.Date("(u'9', u'2005', u'06')", format = "(u'%d', u'%Y', u'%m")
# [1] "2005-06-09"