如何将此格式的日期转换为R中的日期类? [英] How do I convert dates in this format to a date class in R?

查看:135
本文介绍了如何将此格式的日期转换为R中的日期类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数据的电子表格,日期如下所示:

I have a spreadsheet full of data with dates that look like this:

   Mon Jul 16 15:20:22 +0000 2012

有没有办法将这些转换为R日期(最好是PST),而不使用正则表达式有没有别的办法?

Is there a way to convert these to R dates (preferably PST) without using regular expression or is there no other way? I'd appreciate ideas on doing this conversion efficiently.

推荐答案

当然,只需使用 strptime()从字符串解析时间:

Sure, just use strptime() to parse time from strings:

R> strptime("Mon Jul 16 15:20:22 +0000 2012", 
+           format="%a %b %d %H:%M:%S %z %Y")
[1] "2012-07-16 10:20:22 CDT"
R> 

它使用我的本地时区(CDT)。如果你是太平洋,你可以在

which uses my local timezone (CDT). If yours is Pacific, you can set it explicitly as in

R> strptime("Mon Jul 16 15:20:22 +0000 2012", 
+           format="%a %b %d %H:%M:%S %z %Y", tz="America/Los_Angeles")
[1] "2012-07-16 08:20:22 PDT"
R> 

看起来正确,UTC为7小时增量。

which looks right with a 7 hour delta to UTC.

这篇关于如何将此格式的日期转换为R中的日期类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆