R:日期格式转换

时间:2015-06-29 16:53:27

标签: r date

我的Twitter数据采用以下日期格式:

date <- "Wed Jan 07 20:57:02 +0000 2015"

有人知道这是否是标准日期格式?以及任何可以将其转换为m-d-y的包?

2 个答案:

答案 0 :(得分:6)

在基数R中,您可以使用formatstrptimestrptime将字符表示转换为POSIXlt,format将生成的POSIXlt类对象转换为所需格式的字符表示。有关详细信息,请参阅?strptime。还有DateTimeClasses的帮助页面。

对于这种情况,您可以使用:

format(
  strptime(date, format="%a %b %d %T %z %Y"),
  "%m-%d-%Y"
  )
#[1] "01-07-2015"

格式为:

abbreviated day of the week                 %a
abbreviated month                           %b
day of month as decimal number              %d
hours:minutes:seconds                       %T
signed offset from UTC in hours and minutes %z
year with century                           %Y

答案 1 :(得分:2)

strptime("Jan 07 20:57:02 +0000 2015", format="%b %d %T %z %Y")

因为它不能在我的语言环境中工作,所以减少了工作日。

相关问题