Converting date with additional characters in format

后端 未结 3 1225
说谎
说谎 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 20:53

    Try this:

    as.Date(gsub("[u',()]","",my_data$date), format = '%d %Y %m')
    

    Example with a single string:

    d <- "(u'9', u'2005', u'06')"
    d <- gsub("[u',()]","",d)
    d.date <- as.Date(d, "%d %Y %m")
    

    Result:

    d.date
    [1] "2005-06-09"
    

提交回复
热议问题