Datetime format in r
Table of contents
- Date or Datetime ๋ณํ ๊ฐ์
- String (charactor) -> Date (Date)
- String (charactor) -> Datetime (POSIXct, POSIXt)
- Date, DateTime (Date, POSIXct) -> String (charactor)
- Date -> DateTime
- String-> time
- String-> ๋ ์งํ์String (ISO8601ํ์ค์ ๋ง๊ฒ)
- <์ฐธ๊ณ > as.POSIXct ์ ๊ดํด
- 3. ๋ ์ง/์๊ฐ ํฉํค์ง ํ์ฉ
Date or Datetime ๋ณํ ๊ฐ์
http://statkclee.github.io/data-science/ds-date-basics.html
https://www.cyclismo.org/tutorial/R/time.html#time-data-types
๋ ์ง ์ ์๊ฐ ์ฐจ์ด
R์์ ๋ ์ง์ ์๊ฐ์ ๊ฐ๊ฐย ย ๋ค๋ฅธ ํด๋์ค๋ฅผ ํตํด ๊ตฌํ๋๋๋ฐ,
– ๋ ์ง(Date)๋ย Dateย ํด๋์ค์ด๊ณ , (1970-01-01์ ๊ธฐ์ค์ผ๋ก R ๋ด๋ถ์ ์ผ๋ก) ์ ์ํ์ผ๋ก ์ ์ฅ๋๊ณ ,
– ์๊ฐ(Time)์ POSIXct,ย POSIXltย ํด๋์ค์ด๊ณ , (1970-01-01์ ๊ธฐ์ค์ผ๋ก R ๋ด๋ถ์ ์ผ๋ก) ย ์ด๋จ์๋ก ์ ์ฅ๋๋ค.
KST (Korea Standard Time),
GMT (Greenwich Mean Time) : ย time zone
= UTC (Coordinated Universal Time ) : time standard
### ํ์ฌ์๊ฐ > Sys.time() [1] "2017-09-20 16:56:08 KST" > Sys.time() %>% class [1] "POSIXct" "POSIXt" > Sys.time() %>% as.number() [1] 1578494887 ### ํ์ฌ TimeZone > Sys.timezone(location = T) [1] "Asia/Seoul" ### ์ฐ์ฐ์ ์ํด, 1970๋ 1์1์ผ์ ๊ธฐ์ค์ผ๋ก ์ด๋จ์๋ก ํ์ฐ ### (๋ ์ง, ์ฐ๋๋จ์๋ก ํ์ฐํ ์ ์์ง๋ง, ๋ฐ๋์ฐ์ฐ์ ๋ถ๊ฐ) > dtime <- "1970-01-01 00:00:06" %>% as.POSIXct(tz="GMT") > dtime %>% as.numeric() [1] 6 # Asia/Seoul = GMT+9 > dtime <- "1970-01-01 00:00:00" %>% as.POSIXct(tz="Asia/Seoul") > dtime %>% as.numeric() [1] -32400 > dday <- "1970-01-02" %>% as.Date() > dday %>% as.numeric() [1] 1 # hour*min*sec 24*60*60=86400 > dtime <- "1970-01-02 00:00:00" %>% as.POSIXct(tz="GMT") > dtime %>% as.numeric() [1] 86400
00:00:00
disappears
> dtime <- "2017-09-20 00:00:00" > dtime %>% as.POSIXct(tz=Sys.timezone()) [1] "2017-09-20 KST" > > print.POSIXct <- function(x,...)print(format(x,"%Y-%m-%d %H:%M:%S")) > dtime %>% as.POSIXct(tz=Sys.timezone()) [1] "2017-09-20 00:00:00"
๋ณํ ์์ ์์
- ISO 8601ย ํ์ค๋ฌธ์์ด(โ1970-01-01โ) ์ ๊ทผ๊ฑฐํ์ฌ ํํ๋ ๋ ์ง/์๊ฐ์ R์์๋ String๋ก ์ธ์ํ๊ณ ,
- ์ด๋ฐ String ๋ฐ์ดํฐ๋ ๋ ์ง/์๊ฐ ๋ณํ ํจ์(as.Date,ย as.POSIXct,ย as.POSIXlt,ย strptime)๋ฅผ ํตํด DateTime ๋ฐ์ดํฐ๋ก ๋ณํํ ํ,
- ๋ ์ง/์๊ฐ ํฉํค์ง(zoo,ย xts,ย lubridate)๋ฅผ ํตํด ์์ ์ ์งํํ๋ค.
String (charactor) -> Date (Date)
lubridate::ymd()
lubridate::as_date()
"2001-10-06" %>% ymd() "2001-10-06 00:00:00" %>% as_date() #[1] "2001-10-06"
as. Date(format=””)
> "06-07-19" %>% as.Date(format="%y-%m-%d") [1] "2006-07-19"
"1970-01-02" %>% as.Date() # "1970-01-02" "1970-01-02" %>% as.Date() %>% class # "Date" "1970-01-02" %>% as.Date() %>% as.numeric() # 1 "1970-01-02" %>% as.Date() %>% typeof() # "double"
Sys.time() %>% format("%B%d, %a, %H:%M") [1] "July07, Tue, 13:49"
String (charactor) -> Datetime (POSIXct, POSIXt)
POSIX :ย Portableย Operatingย Systemย Interfaceย forย Unixย (applicationย programmingย interface)
– ctย : continuous timeย (the number of seconds)
POSIXctย ํด๋์ค๋ ๋งค์ฐ ํฐ ์ ์๋ก ์๊ฐ์ ๋ณด๋ฅผ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ์ ์ฅํ ๋ ์ ์ฉํ๋ค.
– lt :ย list time,ย (keeps the date as a list of time attributes (such as “hour” and “mon”))
POSIXltย ํด๋์ค๋ ย ์์ผ, ๋
, ์, ์ผ ๋ฑ์ ์ ๋ณด๋ฅผ ๋ฆฌ์คํธ ์๋ฃํ์ผ๋ก ย ๋ฆฌ์คํธ ๋ด๋ถ ์์๋ก ์ ์ฅ๋์ด ์ ์ฉํ๋ค.
lubridate::mdy_hms
lubridate::parse_date_time
lubridate::as_datetime()
https://lubridate.tidyverse.org/
https://rawgit.com/rstudio/cheatsheets/master/lubridate.pdf
"01/01/2010 00:00:00" %>% mdy_hms(tz="Asia/Seoul") "01/01/2010 00:00:00" %>% parse_date_time(orders="%m/%d/%Y %H:%M:%S", tz="Asia/Seoul") "2001-10-06 00:00:00" %>% as_datetime() # [1] "2001-10-06 00:00:00"
hour๋จ์ time series ๋ง๋ค๊ธฐ
https://stackoverflow.com/questions/33782218/how-to-create-a-time-series-of-hourly-data
library("lubridate") startDateTime <- ymd_hms("2020-02-28 23:00:00") endDateTime <- ymd_hms("2020-03-01 13:00:00") elapsedHours <- as.numeric(endDateTime- startDateTime)*24 rlt <- startDateTime + hours(0:elapsedHours) rlt # [1] "2020-02-28 23:00:00" "2020-02-29 00:00:00" "2020-02-29 01:00:00" "2020-02-29 02:00:00" "2020-02-29 03:00:00" "2020-02-29 04:00:00" # [7] "2020-02-29 05:00:00" "2020-02-29 06:00:00" "2020-02-29 07:00:00" "2020-02-29 08:00:00" "2020-02-29 09:00:00" "2020-02-29 10:00:00" # ... # [37] "2020-03-01 11:00:00" "2020-03-01 12:00:00" "2020-03-01 13:00:00"
base:: strptime()
๋ฌธ์์ด (character ๋ฒกํฐ)๋ฅผ POSIXlt ํด๋์ค๋ก ๋ณํํ๋ค.
> "1970-01-01 00:00:00" %>% strptime(format="%Y-%M-%d %H:%M:%S") > "1970-01-01 00:00:00" %>% strptime(format="%Y-%M-%d %H:%M:%S") %>% typeof() # [1] "list" > "1970-01-01 00:00:00" %>% strptime(format="%Y-%M-%d %H:%M:%S") %>% attributes() $names [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" "zone" "gmtoff" $class [1] "POSIXlt" "POSIXt"
> strptime("1970-01-01 00:00:00", format="%Y-%M-%d %H:%M:%S") %>% class [1] "POSIXlt" "POSIXt" > strptime("1970-01-01 00:00:00", format="%Y-%M-%d %H:%M:%S") %>% attributes() $names [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" "zone" "gmtoff" $class [1] "POSIXlt" "POSIXt" > strptime("1970-01-01 00:00:00", format="%Y-%M-%d %H:%M:%S") %>% typeof() [1] "list" > strptime("1970-01-01 00:00:00", format="%Y-%M-%d %H:%M:%S") %>% as.numeric() [1] -32400 > strptime("1970-01-01 00:00:00.975", format="%Y-%M-%d %H:%M:%OS") [1] "1970-07-01 00:00:00.975 KST"
as.POSIXct()
(tz="")
"1970-01-01 00:00:06" %>% as.POSIXct(tz="GMT") # "1970-01-01 00:00:06" "1970-01-01 00:00:06" %>% as.POSIXct(tz="GMT") %>% class # "POSIXct" "POSIXt" "1970-01-01 00:00:06" %>% as.POSIXct(tz="GMT") %>% as.numeric() # 6 # Asia/Seoul = GMT+9 # hour*min*sec 24*60*60=86400
(format="")
dtime1 <- "2018-11-01 \\xbf\\xc0\\xc0\\xfc 10:36:07" %>% str_replace("\\xbf\\xc0\\xc0\\xfc", "AM") #dtime2 <- "2018-11-01 \\xbf\\xc0\\xc8\\xc4 10:02:14" %>% str_replace("\\xbf\\xc0\\xc8\\xc4", "PM") dtime1 %>% as.POSIXct(format="%Y-%m-%d %p %I:%M:%S")
์ฐ๋๋ณ ํ๊ท ๊ฐ, ์๋ณ ํ๊ท , ์ผ๋ณ ํ๊ท ๋ฑ์ ๊ตฌํ๊ธฐ ์ํด listํํ๋ก ์๊ฐ์ ๋๋์ด, year, mon, wday๋ฑ์ ํ์ฉํ๋ค.
> as.POSIXlt("1970-01-01 00:00:00") %>% attributes() $names [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" "zone" "gmtoff" $class [1] "POSIXlt" "POSIXt" > as.POSIXlt("1970-01-01 00:00:00") %>% typeof() [1] "list" > as.POSIXlt("1970-01-01 00:00:00") %>% unlist sec min hour mday mon year wday yday isdst zone gmtoff "0" "0" "0" "1" "0" "70" "4" "0" "0" "KST" NA > as.POSIXlt("1970-01-01 00:00:00") %>% unclass() $sec [1] 0 $min [1] 0 $hour [1] 0 $mday [1] 1 $mon [1] 0 $year [1] 70 $wday [1] 4 $yday [1] 0 $isdst [1] 0 $zone [1] "KST" $gmtoff [1] NA
as.POSIXlt()
์ฐ๋๋ณ ํ๊ท ๊ฐ, ์๋ณ ํ๊ท , ์ผ๋ณ ํ๊ท ๋ฑ์ ๊ตฌํ๊ธฐ ์ํด listํํ๋ก ์๊ฐ์ ๋๋์ด, year, mon, wday๋ฑ์ ํ์ฉํ๋ค.
> as.POSIXlt("1970-01-01 00:00:00") %>% attributes() $names [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" "zone" "gmtoff" $class [1] "POSIXlt" "POSIXt" > as.POSIXlt("1970-01-01 00:00:00") %>% typeof() [1] "list" > as.POSIXlt("1970-01-01 00:00:00") %>% as.numeric() [1] -32400 > as.POSIXlt("1970-01-01 00:00:00") %>% unlist sec min hour mday mon year wday yday isdst zone gmtoff "0" "0" "0" "1" "0" "70" "4" "0" "0" "KST" NA > as.POSIXlt("1970-01-01 00:00:00") %>% unclass() $sec [1] 0 $min [1] 0 $hour [1] 0 $mday [1] 1 $mon [1] 0 $year [1] 70 $wday [1] 4 $yday [1] 0 $isdst [1] 0 $zone [1] "KST" $gmtoff [1] NA
Date, DateTime (Date, POSIXct) -> String (charactor)
format(), strftime()
### Date -> String > as.POSIXlt("1970-01-01 01:02:03") %>% format("%Y-%M-%d") # [1] "1970-02-01" > as.POSIXlt("1970-01-01 01:02:03") %>% strftime("%Y-%m-%d") # [1] "1970-01-01" ### DateTime -> String > as.POSIXlt("1970-01-01 01:02:03") %>% format("%Y-%M-%d %H:%M:%S") # [1] "1970-02-01 01:02:03" > as.POSIXlt("1970-01-01 01:02:03") %>% format("%y-%m-%d %I:%M:%S %p") # [1] "70-01-01 01:02:03 AM" # strftime is wrapper for format.POSIXlt, and it and format.POSIXct > as.POSIXlt("1970-01-01 01:02:03") %>% strftime("%y-%m-%d %I:%M:%S %p") #[1] "70-01-01 01:02:03 AM"
stringr:: str_to_sentence()
ymd_hms("1970-01-01 01:02:03 KST") %>% str_to_sentence()
Date -> DateTime
> tt <- c("09:12", "17:01") [1] "09:12" "17:01" > tt %>% class [1] "character" > library(chron) > times(str_c(tt, ":00.100")) [1] 09:12:00 17:01:00 > times(str_c(tt, ":00.100")) %>% class [1] "times"
String-> time
dtime1 <- "2018-11-01 \\xbf\\xc0\\xc0\\xfc 10:36:07" %>% str_replace("\\xbf\\xc0\\xc0\\xfc", "AM") dtime2 <- "2018-11-01 \\xbf\\xc0\\xc8\\xc4 10:02:14" %>% str_replace("\\xbf\\xc0\\xc8\\xc4", "PM") dtime1 %>% as.POSIXct(format="%Y-%m-%d %p %I:%M:%S")
String-> ๋ ์งํ์String (ISO8601ํ์ค์ ๋ง๊ฒ)
dtime1 <- "2018-11-01 \\xbf\\xc0\\xc0\\xfc 10:36:07" %>% str_replace("\\xbf\\xc0\\xc0\\xfc", "AM") dtime2 <- "2018-11-01 \\xbf\\xc0\\xc8\\xc4 10:02:14" %>% str_replace("\\xbf\\xc0\\xc8\\xc4", "PM") dtime1 %>% as.POSIXct(format="%Y-%m-%d %p %I:%M:%S")
์ฌ๋ฌ ํฌ๋ฉง์ ๋ ์ง๋ฐ์ดํฐ๋ฅผ (format์ ์์์ด) POSIX๋ Date ํฌ๋ฉง์ convertingํด์ค๋ค.
> anytime::anydate(c("19760811","1976-08-12","08-13-1976")) [1] "1976-08-11" "1976-08-12" "1976-08-13" > anytime::anydate(c("1976-Aug-14","1976aug15","16aug1976","Aug-17-1976","18 Aug 1976")) [1] "1976-08-14" "1976-08-15" "1976-08-16" "1976-08-17" "1976-08-18" > anytime::anydate(c("19th August 1976")) [1] NA
http://dirk.eddelbuettel.com/code/anytime.html
result <- dd$tot_time %>% lapply( function(x){ #x <- tstr[10] tstr <- str_split(x,"d ")[[1]] if(length(tstr)>1) { dtime <- str_c("2018-07-18"," ",tstr[2]) dtime <- strptime(dtime, "%Y-%d-%d %H:%M:%OS") + days(tstr[1]) }else{ dtime <- str_c("2018-07-18",tstr) dtime <- strptime(dtime, "%Y-%d-%d %H:%M:%OS") } dtime }) result <- do.call("c", result) library(anytime) dd[ , tot_time0:= anytime(result)]
<์ฐธ๊ณ > as.POSIXct ์ ๊ดํด
POSIXct ํ์
์ ๋ด๋ถ์ ์ผ๋ก๋ ์ ์๋ฅผ ์ ์ฅํ๊ณ ์๊ธฐ ๋๋ฌธ์,
Plottingํ ๋ ์ซ์์ฒ๋ผ ์์ฐจ์ ์ผ๋ก ํ์ฉํ ์ ์์ผ๋ฉด, ggplot์์ scale_x_date / scale_x_time/ scale_x_datetime ๋ฅผ ํ์ฉํ์ฌ ํํ๊ฐ๋ฅํ๋ค.
DT <- data.table( date = Sys.Date() - 0:29, price = runif(30) ) p <- DT %>% ggplot(aes(date, price)) + geom_line() p + scale_x_date(date_breaks="1 week", date_labels="%W",date_minor_breaks="1 day")
> ymd_hms("2020-01-22 13:01:59.23") %>% ceiling_date("day") #[1] "2020-01-23 00:00:00" > ymd_hms("2020-01-22 13:01:59.23") %>% round_date("day") #[1] "2020-01-23 00:00:00" > ymd_hms("2020-01-22 13:01:59.23") %>% floor_date("day") #[1] "2020-01-22 00:00:00"
## strings into POSIXct date-time. library("lubridate") "01/01/2010 00:00:00" %>% parse_date_time("%m/%d/%Y %H:%M:%S", tz="Asia/Seoul") "01/01/2010 00:00:00" %>% mdy_hms(tz="Asia/Seoul") %>% attributes() 20101215 %>% ymd() ## POSIXct date-time into strings/ numeric ymd_hms("2010-12-13 15:30:30 KST") %>% force_tz("America/Chicago") %>% with_tz() ymd_hms("2010-12-13 15:30:30 KST", tz="Asia/Seoul") %>% with_tz() time <- ymd_hms("2010-12-13 15:30:30", tz="Asia/Seoul") time %>% with_tz() # ์ฐ์ฐ > dmy("14/10/1979") %>% month() [1] 10 > diffdd <- (ymd_hms("2010-12-13 16:30:30") - ymd_hms("2010-12-13 15:10:20")) > diffdd %>% as.period [1] "1H 20M 10S" > diffdd %>% as.period %>% as.numeric(unit="hours") [1] 1.336111
3. ๋ ์ง/์๊ฐ ํฉํค์ง ํ์ฉ
String์์ ๋ณํ๋ ๋ ์ง/์๊ฐ ๊ฐ์ฒด๋ ์ฌ๋ฌ ํฉํค์ง๋ฅผ ํ์ฉ๊ฐ๋ฅํ๋ค.