Convert yyyymmdd string to Date class in R

后端 未结 4 2147
时光取名叫无心
时光取名叫无心 2020-11-29 07:03

I would like to convert these dates with format YYYYMMDD to a Date class.

dates <- data.frame(Date = c(\"20130707\", \"20130706\", \"20130705\", \"2013070         


        
4条回答
  •  爱一瞬间的悲伤
    2020-11-29 08:02

    An extra conversion to characters works for me:

    dates<-as.Date(as.character(dates),format="%Y%m%d")
    

    Without the conversion the following error occurs:

    dates<-as.Date(dates,format="%Y%m%d")
    Error in as.Date.numeric(dates, format = "%Y%m%d") : 
      'origin' must be supplied
    

    Different error but this might help, works for POSIXct too, paste date and hours, format %Y%m%d%H

提交回复
热议问题