Converting date with additional characters in format

后端 未结 3 1248
说谎
说谎 2021-01-24 20:13

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 (,

3条回答
  •  梦谈多话
    2021-01-24 21:13

    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"
    

提交回复
热议问题