2015-06-29 162 views
-1

我有以下日期格式的Twitter數據:R:日期格式轉換

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

有誰知道這是一個標準的日期格式?和任何可能能夠將其轉換爲m-d-y的軟件包?

+1

'?strptime'會幫你轉換。看起來像「dayofweek月日的timein24hformat時區年」 – mts

回答

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 
2
strptime("Jan 07 20:57:02 +0000 2015", format="%b %d %T %z %Y") 

我砍平日,因爲它不會在我的區域工作。