Converting date with additional characters in format

后端 未结 3 1261
说谎
说谎 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

    If it is character class, you can try:

    library(lubridate)
    
    test <- c("u'9'", "u'2005'", "u'06'")
    
    dym(paste(gsub("u|'", "", test), collapse = "/"))
    [1] "2005-06-09 UTC"
    

    Here I use lubridate to convert the string where I removed "u" and the ' character into time format. The collapse character I used in paste is arbitrary, lubridate can handle pretty much anything as a separator between date parts.

提交回复
热议问题