Datetime in r

Published by onesixx on

1. Date변환

as.Date("2016-08-19")     # Date
#"2016-08-19"

library(lubridate)
ymd("2016-08-19")         # Date
make_date(2016,8,19)
# "2016-08-19"
identical(as.POSIXct("2019-08-19") , make_date(2019,8,19))
# [1] FALSE

2. POSIXct 변환

as.POSIXct("2016-08-19")  # "POSIXct" "POSIXt" 
#"2016-08-19 UTC"

1. Date 변환

###### "Date" to Numeric
dday <- "1970-01-02" %>% as.Date()
dday %>% as.numeric()
# [1] 1


###### "POSIXct" "POSIXt" to Numeric
Sys.time()                     
# [1] "2021-05-23 06:56:48.672417 UTC"
Sys.time() %>% as.numeric()    
# [1] 1578494887

dtime <- "1970-01-01 00:00:06" %>% as.POSIXct(tz="GMT")  #[1] "POSIXct" "POSIXt" 
# [1] "1970-01-01 00:00:06 GMT"
dtime %>% as.numeric()
# [1] 6

# Asia/Seoul = GMT+9
dtime <- "1970-01-01 00:00:00" %>% as.POSIXct(tz="Asia/Seoul")
# [1] "1970-01-01 KST"
dtime %>% as.numeric()
# [1] -32400
# hour*min*sec   24*60*60=86400
dtime <- "1970-01-02 00:00:00" %>% as.POSIXct(tz="GMT")
dtime %>% as.numeric()
# [1] 86400

as.Date()

String->날짜변환

변환 함수을 통해 String 데이터를 DateTime 객체로 변환한다.
먼저 ISO 8601 기준에 맞는 문자열(“1970-01-01”)을 as.Date 함수를 통해 날짜 객체로 변환한다. (내부적으로 보면 정수 0 으로 저장된다) 

> "06-07-19" %>% as.Date(format="%y-%m-%d")
[1] "2006-07-19"
> as.Date("1970-01-01") %>% attributes()
$class
[1] "Date"
> as.Date("1970-01-01") %>% typeof()
[1] "double"
> as.Date("1970-01-01") %>% as.numeric()
[1] 0

lubridate::ymd() mdy()…

library(lubridate)

#nt[ ,date %>% day()]
nt[ ,date %>% mday()]
nt[ ,date %>% qday()]
nt[ ,date %>% yday()]
nt[ ,date %>% wday(label=T)]

https://onesixx.com/lubridate/

cf> strptime

  if (class(mydates)=="character" | class(mydates)=="factor"){
    mydates <- strptime(mydates, date.form)
  }

> date.form
[1] "%Y-%m-%d"

2. POSIXct DateTime 변환

as.POSIXct()

We do not recommend use of POSIXlt at all because it uses 40 bytes to store one date.
as.POSIXct("2016-08-19")  # "POSIXct" "POSIXt" 
#"2016-08-19 UTC"
identical( ymd("2016-08-19"),         make_date(2016,8,19) ) # TRUE
identical( as.Date("2016-08-19") ,    make_date(2016,8,19) ) # TRUE
identical( as.POSIXct("2016-08-19") , make_date(2016,8,19) ) # FALSE

String-> DateTime 변환

> as.POSIXct("1970-01-01 00:00:00") %>% attributes()
$class
[1] "POSIXct" "POSIXt" 
$tzone
[1] ""
> as.POSIXct("1970-01-01 00:00:00") %>% typeof()
[1] "double"
> as.POSIXct("1970-01-01 00:00:00") %>% as.numeric()
[1] -32400

> as.POSIXct("1970-01-01 00:00:00") %>% unclass()
[1] -32400
attr(,"tzone")
[1] ""
https://3months.tistory.com/289

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")

format(), strftime()

DateTime(POSIXct) -> 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"

Lubridate()

https://onesixx.com/lubridate/

String-> DateTime 변환

## 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

Date sequence 만들기

Sys.Date()-1:10

seq(as.Date("2016-01-01"), by="days", length=5)
seq(as.Date("2016-01-01"), by="months", length=5) %>% as.yearmon()

#using ts object
ts( runif(5), start=c(2016, 3), frequency=12) 

ts object에서 Date뽑기

as.Date(as.yearmon(time(co2)))
co2 %>% time() %>% as.yearmon %>% as.Date()
Categories: tsReshaping

onesixx

Blog Owner

Subscribe
Notify of
guest

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