# 用 lubridate 處理時間序列(time series)中的日期

``````library(dplyr)
library(ggplot2)
library(lubridate)
``````

## 實作時間序列圖

``````seaice.09 <- read.csv("2016-12-10-seaice_2009.csv", stringsAsFactors = FALSE) %>% select(-Missing)
``````
``````##   Year Month Day Extent hemisphere
## 1 2009     1   1 13.189      north
## 2 2009     1   2 13.180      north
## 3 2009     1   3 13.267      north
``````

``````seaice <- seaice.09 %>%
mutate(date = (paste(Year, Month, Day, sep="-") %>% ymd()))
``````
``````##   Year Month Day Extent hemisphere       date
## 1 2009     1   1 13.189      north 2009-01-01
## 2 2009     1   2 13.180      north 2009-01-02
## 3 2009     1   3 13.267      north 2009-01-03
``````

``````class(seaice\$date)
``````
``````## [1] "Date"
``````
``````seaice %>% ggplot(aes(x=date, y=Extent, color=hemisphere)) + geom_point()
``````

## 轉換日期為十進位數值

ggplot2在作圖時可以直接用日期的資料格式，但如果要進行迴歸分析，就必須把日期轉換成數值格式才行。好在 lubridate 中的 `decimal_date()` 可以幫我們快速轉換。(如果要把數值轉為日期則是`date_decimal()`)

``````seaice %>% mutate(num.date = decimal_date(date)) %>% head(3)
``````
``````##   Year Month Day Extent hemisphere       date num.date
## 1 2009     1   1 13.189      north 2009-01-01 2009.000
## 2 2009     1   2 13.180      north 2009-01-02 2009.003
## 3 2009     1   3 13.267      north 2009-01-03 2009.005
``````

## 其他日期排列組合

``````example.data <- data.frame(
id = c("A","B","C"),
before.date = c("06/04/2010","06/11/2010","06/23/2010")
)

example.data %>% mutate(after.date = mdy(before.date))
``````
``````##   id before.date after.date
## 1  A  06/04/2010 2010-06-04
## 2  B  06/11/2010 2010-06-11
## 3  C  06/23/2010 2010-06-23
``````

lubridate在整理日期時可以處理許多種類型的字串，包括20090614或是2009/06/14都能轉換成POSIX標準的年-月-日格式，相當實用。

Written on December 10, 2016