简体   繁体   中英

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

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.

Also note that as your string is in a standard format, you can also use directly as.POSIXlt :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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