Change the year in a datetime object in R?

前端 未结 3 700
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 08:53

I have the following:

\"0014-06-30\"

And I\'d like to change it to:

\"0000-06-30\"

How would I do this in

3条回答
  •  天命终不由人
    2020-12-18 09:13

    I couldn't get the specific format by converting to as.Date. A regex option would be

    str1 <- "0014-06-30"
    as.Date(str1)
    #[1] "14-06-30"
    
    sub('\\d{2}(?=-)', '00', str1, perl=TRUE)
    #[1] "0000-06-30"
    

提交回复
热议问题