Datetime format in r

Published by onesixx on

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์—์„œ ๋ณ€ํ™˜๋œ ๋‚ ์งœ/์‹œ๊ฐ„ ๊ฐ์ฒด๋Š” ์—ฌ๋Ÿฌ ํŒฉํ‚ค์ง€๋ฅผ ํ™œ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

zoo

xts

Categories: R Reshaping

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x