為什麼用 lubridate?

在 R 中處理日期與時間

Charlotte Wickham

Instructor

lubridate

  • 讓你在 R 中輕鬆處理日期時間!
  • tidyverse 套件
    • 與內建日期時間物件相容良好
    • 為人而設計,不是為電腦
  • 與其他 tidyverse 套件相容良好
  • 不論底層物件為何,行為一致
在 R 中處理日期與時間

解析多種格式

ymd("2013-02-27")
"2013-02-27"
dmy("27/2/13")
"2013-02-27"
parse_date_time(c("Feb 27th, 2017", "27th Feb 2017"), 
+   order = c("mdy", "dmy"))
"2017-02-27 UTC" "2017-02-27 UTC"
在 R 中處理日期與時間

操作日期時間

# Extract components
akl_daily <- akl_daily %>%
  mutate(
    year = year(date),
    yday = yday(date),
    month = month(date, label = TRUE)
  )

在 R 中處理日期與時間

時間跨度

# A tibble: 131 x 3
            name              period 
           <chr>        <S4: Period> 
1  Elizabeth II 65y 7m 27d 0H 0M 0S 
2      Victoria  63y 7m 2d 0H 0M 0S 
3      George V 25y 8m 14d 0H 0M 0S 
4    George III 19y 0m 28d 0H 0M 0S 
5     George VI 15y 1m 26d 0H 0M 0S 
在 R 中處理日期與時間

其他 lubridate 功能

  • 時區處理
  • 標準格式的快速解析
  • 日期時間輸出
在 R 中處理日期與時間

一起來練習吧!

在 R 中處理日期與時間

Preparing Video For Download...