날짜없는 time
https://departmentfortransport.github.io/R-cookbook/dates-times.html
string –> time
시각
tt <- data.table(timeString = c("0:05:00","1:41:46","1:00:00","3:14:02","1:00:00")) #tt[ , as.POSIXct(timeString)] # Error : 애매한 포멧 tt[ , as.ITime(timeString)] # 클래스 ITime from data.table tt[ , as.difftime(timeString, units="secs")] # 클래스 difftime from base tt[ , as_hms(timeString)] # 클래스 difftime from hms tt[ , as.period(as_hms(timeString))] # 클래스 Period from lubridate tt[ , as.ITime(timeString)] %>% sum() %>% as.numeric() # 초 기준 tt[ , as.difftime(timeString, units="secs")] %>% sum() %>% as.numeric() tt[ , as_hms(timeString)] %>% sum() %>% as.numeric() # 초 기준 tt[ , as.period(as_hms(timeString))] %>% as.numeric(units="seconds") %>% sum()
> tt <- data.table(timeString = c("0:05:00","1:41:46","1:00:00","3:14:02","1:00:00")) timeString 1: 0:05:00 2: 1:41:46 3: 1:00:00 4: 3:14:02 5: 1:00:00 > tt[ , as.difftime(timeString, units="secs")] # 클래스 difftime from base Time differences in secs [1] 300 6106 3600 11642 3600 > tt[ , as_hms(timeString)] # 클래스 difftime from hms 00:05:00 01:41:46 01:00:00 03:14:02 01:00:00 > tt[ , as.period(as_hms(timeString))] # 클래스 Period from lubridate [1] "5M 0S" "1H 41M 46S" "1H 0M 0S" "3H 14M 2S" "1H 0M 0S" > tt[ , as.difftime(timeString, units="secs")] %>% sum() %>% as.numeric() > tt[ , as_hms(timeString)] %>% sum() %>% as.numeric() # 초 기준 > tt[ , as.period(as_hms(timeString))] %>% as.numeric(units="seconds") %>% sum() [1] 25248
시간
timeString <- "24:00:06" # 24시간이 넘어가면 문제가 생김. as.ITime(timeString) as.difftime(timeString, units="secs") as_hms(timeString) as.period(as_hms(timeString))
> timeString <- "24:00:06" > as.ITime(timeString) [1] "00:00:00" > as.difftime(timeString, units="secs") Time difference of NA secs > as_hms(timeString) NA Warning message: Lossy cast from <character> to <hms> at position(s) 1 > as.period(as_hms(timeString)) [1] NA Warning message: Lossy cast from <character> to <hms> at position(s) 1 > tt[ , as.ITime(timeString)] %>% sum() %>% as.numeric() # 초 기준 Error in tt[, as.ITime(timeString)] : incorrect number of dimensions
uF_timeStringToSec <- function(colNm){ colNm %>% lapply( function(x){ #x="0:06:00" apart <- str_split(x, ":")[[1]] %>% as.numeric() ret <- apart * c(60*60, 60, 1) return(sum(ret)) }) %>% unlist() } # uF_timeStringToSec <- function(colNm){ # colNm %>% lapply( function(x){ #x="0:05:00" # apart <- str_split(x, ":")[[1]] # h <- as.numeric(apart[1]) # m <- as.numeric(apart[2]) # s <- as.numeric(apart[3]) # ret <- h*60*60 + m*60 + s # return(ret) # }) %>% unlist() # } tt[ , uF_timeStringToSec(timeString)]
as.difftime
https://onesixx.com/lubridate/
hms
https://campus.datacamp.com/courses/working-with-dates-and-times-in-r/problems-in-practice?ex=5
hh:mm:ss format
- 'difftime' class를 기본
- 자정부터 second의 numeric vector (digits.secs 옵션으로 micro secound까지 표현 가능)
library(lubridate) library(hms) # number ===> hms, difftime > hms(56, 18, 13) 13:18:56 > hms(56, 18, 13, 1) 37:18:56 > hms(hours=13, minutes=18, seconds=56) 13:18:56 > hms(hours=1:4, minutes=seq(0,60,by=20), seconds=rep(1,4)) 01:00:01 02:20:01 03:40:01 05:00:01 > as_hms(1) # same parse_hms 00:00:01 > as_hms("12:34:56") 12:34:56 > parse_hm("12:34") # there is no as_hm 12:34:00 > as_hms(Sys.time()) 03:41:42.247855 > as_hms("13:18:56") 13:18:56 > as_hms("13:18:56") %>% as.POSIXct() [1] "1970-01-01 13:18:56 UTC" ### 연산 > hms(56, 18, 13, 1) - hms(56, 18, 13) Time difference of 86400 secs > (hms(56, 18, 13, 1) - hms(56, 18, 13)) %>% as_hms() 24:00:00 > (hms(56, 18, 13, 1) + hms(56, 18, 13)) %>% as_hms() 50:37:52 ### Priod library(lubridate) t1 <- hm("10:38") t1 [1] "10H 38M 0S" t2 <- hm("10:39") t1 - t2 [1] "-1M 0S" ### hms, difftime ==> POSIXct > hms(56, 18, 13) %>% as.POSIXct() [1] "1970-01-01 13:18:56 UTC" > hms(56, 18, 13, 1) %>% as.POSIXct() [1] "1970-01-02 13:18:56 UTC" > as_hms(1) %>% as.POSIXct() [1] "1970-01-01 00:00:01 UTC"
cron
https://stackoverflow.com/questions/22659947/r-how-to-handle-times-without-dates
package chron
requires extra work to add seconds, and time subtraction that results in negative values is no longer formatted as a time
library(chron) >t1 = times(paste0("10:38",":00")) >t2 = times(paste0("10:39",":00")) > t1-t2 [1] -0.0006944444 > t2-t1 [1] 00:01:00
fasttime
https://cran.r-project.org/web/packages/fasttime/fasttime.pdf
Fast Utility Function for Time Parsing and Conversion
연산에서 period vs. duration
https://itoner.tistory.com/11 [공돌이 일기]
"기간" period, duration, term
period
시작부터 결론까지의 시간의 길이
빙글 빙글 가거나 오거나, 반복있는 '시작과 끝'같은 이미지의 "기간"
duration
지속되는 동안 '이라는 뜻의 기간
endure는 "지속" during (~ 사이)을 이미지
term
"처음부터 결론까지의 시간 , 기간에 특히 공식적으로 정해진 기간
임기이나 계약 기간이나 징역 연수 등에 쓰임.
term의 어원은 '가장자리' '경계' '한계'의 의미에서 determine가 "경계를 명확하게 (de) 결정 '이라는 의미
term도"명확하게 정해진'이라는 의미를 담은 '기간' 입니다.
Duration vs. Period in r
minutes(2) # period ## 2 minutes dminutes(2) # duration ## 120s (~2 minutes)
> leap_year(2020) #TRUE 윤년(2.29) 4년에 한번 [1] TRUE > ymd(20200101) + years(1) #Period [1] "2021-01-01" > ymd(20200101) + dyears(1) #Duration [1] "2020-12-31"
한달 더하기
https://stackoverflow.com/questions/14169620/add-a-month-to-a-date
Function %m+%
from lubridate adds one month without exceeding last day of the new month.
d <- ymd("2012-01-31") %m+% months(1)
[1] "2012-02-29"
https://www.zeitverschiebung.net/en/
Time Interval
jsm <- interval(ymd(20110720, tz="Pacific/Auckland"), ymd(20110831, tz="Pacific/Auckland"))