時間資料處理
==此處為文章備份用==
有興趣可至 [https://hackmd.io/@LHB-0222/Rdate ](https://hackmd.io/@LHB-0222/Rdate) 觀看全文 
在R資料處理之中,時常會需要遇到時間格式的處理 實驗數據、股市資料、氣候資訊隨著時間不斷的流逝 各式資料也不斷的生成的情況下時間的判讀就顯得重要 多數時候取得的時間資料就是一長串的數字 如何讓程式讀取時間資料以及對時間資料的處理是今天這文章的重點 # 取得時間資訊
有興趣可至 [https://hackmd.io/@LHB-0222/Rdate ](https://hackmd.io/@LHB-0222/Rdate) 觀看全文 
在R資料處理之中,時常會需要遇到時間格式的處理 實驗數據、股市資料、氣候資訊隨著時間不斷的流逝 各式資料也不斷的生成的情況下時間的判讀就顯得重要 多數時候取得的時間資料就是一長串的數字 如何讓程式讀取時間資料以及對時間資料的處理是今天這文章的重點 # 取得時間資訊
Sys.Date() #當前日期 lubridate::today()
date() #當前系統日期和時間
Sys.time() #當前系統日期和時間 lubridate::now()
Sys.timezone() #有關時區的信息將返回當前時區的名稱
:::success
[1] "2019-12-13"
[1] "Fri Dec 13 21:50:58 2019"
[1] "2019-12-13 21:52:04 CST"
[1] "Asia/Taipei"
:::
head(OlsonNames()) #取得有關時區的信息、時區的名稱
:::success
[1] "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa" "Africa/Algiers"
[5] "Africa/Asmara" "Africa/Asmera"
:::
## 取得特定日期或時間資訊
:::danger
format(date,format):
:::
#輸出年份:
format(Sys.Date(),format="%Y")
format(Sys.Date(),format="%y")
#輸出月份:
format(Sys.Date(),format="%b")
format(Sys.Date(),format="%B")
format(Sys.Date(),format="%m")
#輸出星期:
format(Sys.Date(),format="%A")
format(Sys.Date(),format="%a")
#摻雜文字輸出:
format(Sys.Date(),format="%Y年%m月%d日")
:::success
#輸出年份:
[1] "2019"
[1] "17"
[1] "19"
#輸出月份:
[1] "十二月"
[1] "十二月"
[1] "12"
#輸出星期:
[1] "星期五"
[1] "週五"
#摻雜文字輸出:
[1] "2019年12月13日"
:::
也可以使用其他內置函數處理
weekdays(Sys.Date()) #取得日期物件的星期資料
months(Sys.Date()) #取得日期物件的月份
days(Sys.Date()) #取得日期物件日期
quarters(Sys.Date()) #取得日期物件的季度
# 單日期之文字格式轉換
:::danger
as.Date(x, format)
:::
format參數用於指定輸入的格式,常用的日期格式符號是:
|%y|兩個數字表示的年份(00-99),不帶世紀,例如,數值是18,格式%y,表示2018年|
|---|---|
|%Y|四位數字表示的年份(0000-9999)|
|%m|兩個數字的月份,取值範圍是01-12,或1-12|
|%d|月份中的天,取值範圍是01-31|
|%e|月份中的天,取值範圍是1-31|
|%b|縮寫的月份(Jan,Feb,Mar等)|
|%B|英語月份全名(January、February 、March等)|
|%a|縮寫的星期名(Mon、Tue、Wed、Thur、Fri、Sat、Sun)|
|%A|星期全名|
as.Date('2019-12-02', '%Y-%m-%d')
as.Date('12/02 2019', '%m/%d %Y')
:::success
[1] "2019-12-02"
[1] "2019-12-02"
:::
# 包含時間資料之文字格式轉換
:::danger
as.POSIXlt(x, format)
as.POSIXct(x, format)
:::
**POSIXlt** 是以列表的形式分別儲存年、月、日、时、分、秒資料
**POSIXct** 則是以1970年1月1號開始計算過了幾秒,如果是負數,則是1970-01-01年以前;
正數則是1970年以後。
| %H|小時(24小時制)|
|---|---|
|%I|小時(12小時制)|
|%p|對於12小時制,指定上午(AM)或下午(PM)|
|%M|分鐘|
|%S|秒|as.POSIXlt('2019/12/13 22:12:00', format="%Y/%m/%d %H:%M:%S")
as.POSIXct('2019/12/13 22:12:00', format="%Y/%m/%d %H:%M:%S")
:::success
[1] "2019-12-13 22:12:00 CST"
[1] "2019-12-13 22:12:00 CST"
:::
比如2019/12/13 22:12:00相比於1970年1月1號經過了1576246320秒unclass(as.POSIXct('2019/12/13 22:12:00', format="%Y/%m/%d %H:%M:%S"))
:::success
[1] 1576246320
attr(,"tzone")
[1] ""
:::unclass(as.POSIXlt('2019/12/13 22:12:00', format="%Y/%m/%d %H:%M:%S"))
:::success
$sec
[1] 0
$min
[1] 12
$hour
[1] 22
$mday
[1] 13
$mon
[1] 11
$year
[1] 119
$wday
[1] 5
$yday
[1] 346
$isdst
[1] 0
$zone
[1] "CST"
$gmtoff
[1] NA
:::
# 時間格式之運算
不同時間格式間可以進行相減但無法進行相加
```R=
D1 <- 0="" 10:12:00="" 12:00:00="" 12:01:00="" 12:01:56="" 12:01="" 12="" 16="" 1="" 2019="" 223344000="" 22:12:00="" 22:12:01="" 22:36:00="" 23:12:00="" 24="" 2="" 30="" 31="" 350="" 50="" 56="" 60="" :::="" :::danger="" :::success="" :="" alid="" and="" are="" as.date="" as.posixct="" as.posixlt="" auto="" base="" bimonth="" binary="" ceiling_date="" cst="" d1-d2="" d1-d3="" d1-d4="" d1="" d2="" d3="" d4="" d5="" d="" day="" days="" days_in_month="" dec="" defined="" difference="" difftime="" dmy="" dmy_hms="" error="" floor_date="" for="" format="%Y/%m/%d %H:%M:%S" h="" halfyear="" hour="" hours="" in="" install.packages="" is="" library="" lubridate="" m="" mday="" mdy="" mdy_hms="" mins="" minute="" month="" not="" objects="" of="" quarter="" round_date="" s="" season="" second="" secs="" t="" time1="" time2="" time="" tz="" unit="day" units="" utc="" wday="" week="" weeks="" x="" y-m-d="" y="" yday="" year="" yeard="" ymd="" ymd_h="" ymd_hm="" ymd_hms="">[lubridate.tidyverse](https://lubridate.tidyverse.org/)
>[Dates and times with lubridate : : CHEAT SHEET(速查表)](https://rawgit.com/rstudio/cheatsheets/master/lubridate.pdf)
>[R for Data Science](https://r4ds.had.co.nz/dates-and-times.html)
[name=Garrett Grolemund、Hadley Wickham]
全文分享至
https://www.facebook.com/LHB0222/
有疑問想討論的都歡迎於下方留言
喜歡的幫我分享給所有的朋友 \o/
有所錯誤歡迎指教

留言
張貼留言