简体   繁体   English

将日期中的数据以小时和分钟转换为R中的日期

[英]Converting the data in year month day hour and minutes to date in R

I am trying to convert the date as factor to date using “as.date” function in R. I have the date in the following format 我试图使用R中的“as.date”函数将日期转换为日期因素。我有以下格式的日期

2008-01-01 02:30

I tried to use the following command : 我尝试使用以下命令:

as.Date(mydata$Date, format="%y-%m-%d  %h:%mm")

Can somebody help me with this ? 有人可以帮我这个吗? I was able to convert the format with no hour but getting difficulty with hour included. 我能够在没有时间的情况下转换格式,但是包括小时的难度。

Thank you. 谢谢。

Your format string is incorrect : 您的格式字符串不正确:

R> strptime("2008-01-01 02:30", format="%Y-%m-%d  %H:%M")
# [1] "2008-01-01 02:30:00"

See ?strptime for the detailed values you can use to define a format. 有关可用于定义格式的详细值,请参阅?strptime

Also note that as your string is in a standard format, you can also use directly as.POSIXlt : 另请注意,由于您的字符串采用标准格式,因此您也可以直接使用as.POSIXlt

R> as.POSIXlt("2008-01-01 02:30")
# [1] "2008-01-01 02:30:00"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM