2015-07-09 52 views
0

我有一個名爲x變量是這樣的:爲什麼不能在tbl_df中將lubridate轉換日期?

x <- structure(list(Time = c("2002-05-07 21:00", "2002-05-08 21:00", 
          "2002-05-09 21:00", "2002-05-10 21:00", 
          "2002-05-11 21:00", "2002-05-13 21:00", 
          "2002-05-14 21:00", "2002-05-15 21:00", 
          "2002-05-16 21:00", "2002-05-17 21:00")), 
       .Names = "Time", class = c("tbl_df", "data.frame"), 
       row.names = c(NA, -10L)) 

現在,我想在字符串轉換中x爲日期,由於x[1,1] %>% lubridate::ymd_hm()讓我對單個元素預期的結果,我認爲下面會做的伎倆:

x %>% lubridate::ymd_hm() 

但它不工作(結果是NA),我得到以下警告:

Warning message: 
    All formats failed to parse. No formats found. 

爲什麼x %>% lubridate::ymd_hm()不按我期望的方式工作,我該怎麼做才能得到我想要的結果?

+2

您的數據已被截斷。 –

+0

@NickK感謝您發現它。我已經修復它。 – mauna

+1

'ymd_hm(x $時間)'工作得很好。你爲什麼會認爲'x%>%lubridate :: ymd_hm()'會知道'Time'列? – hrbrmstr

回答

3

功能mutate有效。

x %>% mutate(Time = ymd_hm(Time)) 
3

這成功(與我不明白的警告,但我懷疑該對象的rownames可能有事情做吧。):

x %>% lubridate::ymd_hm(.$Time) 
[1] NA      "2002-05-07 21:00:00 UTC" "2002-05-08 21:00:00 UTC" 
[4] "2002-05-09 21:00:00 UTC" "2002-05-10 21:00:00 UTC" "2002-05-11 21:00:00 UTC" 
[7] "2002-05-13 21:00:00 UTC" "2002-05-14 21:00:00 UTC" "2002-05-15 21:00:00 UTC" 
[10] "2002-05-16 21:00:00 UTC" "2002-05-17 21:00:00 UTC" 

x[1,1] %>% lubridate::ymd_hm() 
#[1] "2002-05-07 21:00:00 UTC" 
相關問題