lubridate (tidyverse)

Published by onesixx on

http://lubridate.tidyverse.org/
https://rpubs.com/davoodastaraky/lubridate
https://www.r-statistics.com/2012/03/do-more-with-dates-and-times-in-r-with-lubridate-1-1-0/

https://onesixx.com/time/

Date 타입을 더 쉽게 다루기 위한 팩키지

String -> Datetime

여러형태의 string형식으로 표현된 날짜데이터를 Date형식으로 parsing할수 있다. 일관된format의 Date타입의 날짜시간데이터를 얻을 수 있다.
또한 Date형식의 데이터에서 필요한 요소를 추출할 수도 있다.

ymd_hms()

>  "2016-08/10"     %>% class()     # [1] "character"

> ymd("2016-08/10") %>% class()     # [1] "Date"
> mdy("8.19.16")
[1] "2016-08-19"
> mdy("September 12, 2001")
[1] "2001-09-12"
> "1995-08-19 14:00:00"  # [1] "character"

> specialDay <- ymd_hms("1995-08-19 14:00:00", tz="Asia/Seoul")  # [1] "POSIXct" "POSIXt" 
[1] "1995-08-19 14:00:00 KST"

> with_tz(specialDay, tzone='America/Los_Angeles')               # [1] "POSIXct" "POSIXt" 
[1] "1995-08-18 22:00:00 PDT"

Datetime -> String

year, month, day….

y <-  year(now())
m <- month(now())
d <-   day(now())
> specialDay <- ymd_hms("1995-08-19 14:00:00", tz="Asia/Seoul")  # [1] "POSIXct" "POSIXt" 
[1] "1995-08-19 14:00:00 KST"

# second(), minute(), hour(), day(), wday(), yday(), week(), month(), year(), tz()
> month(specialDay)              # [1] "numeric"
[1] 8
> wday(specialDay, label=TRUE)   # [1] "ordered" "factor"
[1] Sat
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat

Datetime -> Datetime

Round, floor and ceiling methods

https://rdrr.io/cran/lubridate/man/round_date.html
x <- ymd_hms("2006-08-19 12:01:59.63")

  round_date(x, "second") #"2006-08-19 12:02:00 UTC"
  floor_date(x, "second") #"2006-08-19 12:01:59 UTC"
ceiling_date(x, "second") #"2006-08-19 12:02:00 UTC"

x <- ymd_hms("2006-08-19 12:08:31")
round_date(x, "minute") #"2006-08-19 12:09:00 UTC"
round_date(x, "5 mins") #"2006-08-19 12:10:00 UTC"
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