根据R将日期列从csv转换为日期时出现的问题

问题描述

以下是我尝试转换从csv读取的日期列时遇到的错误

data<-read.csv("data.csv",stringsasfactors=FALSE) #data<-read.table("data.csv",sep=",",header=TRUE)

as.Date(data$Date,"%Y-%d-%m")

错误

error in as.date.default(x ...) do not kNow how to convert 'x' to class date

enter image description here

解决方法

如果日期看起来像30-06-2003,那么您需要相应地调整阅读日期的格式。这样,我的意思是正确的格式为"%d-%m-%Y",即 day-month-4 digit ye​​ar ,而不是"%Y-%d-%m"

尝试此行

as.Date(data$Date,format = "%d-%m-%Y")