lubridate: Make dealing with dates a little easier

日付データの操作を簡単にする

> library(lubridate)

Attaching package: 'lubridate'
The following object is masked from 'package:haven':

    hms
The following object is masked from 'package:base':

    date

バージョン: 1.5.6


関数名 概略
%m+% Add and subtract months to a date without exceeding the last day of the new month
%within% Tests whether a date or interval falls within an interval
DateUpdate Changes the components of a date object
Duration-class Duration class
Interval-class Interval class
Period-class Period class
Timespan-class Timespan class
add_epoch_to_date Add epochs to dates
am Does date time occur in the am or pm?
as.duration Change an object to a duration.
as.interval Change an object to an interval.
as.period Change an object to a period.
ceiling_date Round date-times up.
date_decimal Converts a decimal to a date.
days_in_month Get the number of days in the month of a date-time.
decimal_date Converts a date to a decimal of its year.
dseconds Quickly create exact time spans.
dst Get Daylight Savings Time indicator of a date-time.
duration Create a duration object.
fit_to_timeline Fit a POSIXlt date-time to the timeline
floor_date Round date-times down.
force_tz Replace time zone to create new date-time
guess_formats Guess formats from the supplied date-time character vector.
here The current time in your local timezone
hm Create a period with the specified number of hours and minutes
hms Create a period with the specified hours, minutes, and seconds
hour Get/set hours component of a date-time.
int_aligns Test if two intervals share an endpoint
int_diff Extract the intervals within a vector of date-times
int_end Access and change the end date of an interval
int_flip Flip the direction of an interval
int_length Get the length of an interval in seconds
int_overlaps Test if two intervals overlap
int_shift Shift an interval along the timeline
int_standardize Ensures all intervals in an interval object are positive
int_start Access and change the start date of an interval
is.Date Is x a Date object?
is.POSIXt Is x a POSIXct or POSIXlt object?
is.difftime Is x a difftime object?
is.duration Is x a duration object?
is.instant Is x a date-time object?
is.interval Is x an Interval object?
is.period Is x a period object?
is.timespan Is x a length of time?
lakers Lakers 2008-2009 basketball data set
leap_year Is a year a leap year?
lubridate Dates and times made easy with lubridate
lubridate_formats Lubridate format orders used in 'stamp'
make_difftime Makes a difftime object from a given number of seconds
minute Get/set minutes component of a date-time.
month Get/set months component of a date-time.
ms Create a period with the specified number of minutes and seconds
new_difftime Create a difftime object.
new_duration Create a duration object.
new_epoch Create an epoch object
new_interval Create an interval object.
new_period Create a period object.
now The current time
olson_time_zones Names of available time zones
origin 1970-01-01 UTC
parse_date_time Parse character and numeric date-time vectors with user friendly order formats.
period Create a period object.
period_to_seconds Convert a period to the number of seconds it appears to represent
pretty_dates Computes attractive axis breaks for date-time data
quarter Get the fiscal quarter of a date-time.
rollback Roll back date to last day of previous month
round_date Rounding for date-times.
second Get/set seconds component of a date-time.
seconds Quickly create relative timespans.
seconds_to_period Contrive a period from a given number of seconds
stamp Format dates and times based on human-friendly templates.
sundays Quickly create common epoch objects
timespan Description of time span classes in lubridate.
today The current date
tz Get/set time zone component of a date-time.
wday Get/set days component of a date-time.
week Get/set weeks component of a date-time.
with_tz Get date-time in a different time zone
yday Get/set days component of a date-time.
year Get/set years component of a date-time.
ymd Parse dates according to the order in that year, month, and day elements appear in the input vector.
ymd_hms Parse dates that have hours, minutes, or seconds elements.

DateUpdate

日時オブジェクトの更新

Arguments

  • object
  • years
  • months
  • ydays
  • wdays
  • mdays
  • days
  • hours
  • minutes
  • seconds
  • tsz
  • ...
> (x <- now())
[1] "2016-05-20 08:13:46 JST"
> update(x, year = 2010, month = 1, mday = 1)
[1] "2010-01-01 08:13:46 JST"
> update(x, month = 1, mday = 1)
[1] "2016-01-01 08:13:46 JST"

am

午前と午後の時間判定

> am(x)
[1] TRUE
> pm(x)
[1] FALSE

ceiling_date

日時オブジェクトの丸め込み。丸め込みの判定は下位単位によって決まる。

ref) floor_date

> (x <- now())
[1] "2016-05-20 08:13:46 JST"
> ceiling_date(x, "second")
[1] "2016-05-20 08:13:47 JST"
> ceiling_date(x, "hour")
[1] "2016-05-20 09:00:00 JST"
> ceiling_date(x, "year")
[1] "2017-01-01 JST"

date_decimal

ref) decimal_date()

> date <- ymd("2015-10-10")
> decimal_date(date) %>% date_decimal()
[1] "2015-10-09 23:59:59 UTC"

decimal_date

日付オブジェクトを連続値に変換

ref) date_decimal()

> date <- ymd("2015-10-10")
> decimal_date(date)
[1] 2015.773

dminutes

分を秒表示に変換

> dminutes(2)
[1] "120s (~2 minutes)"

floor_date

日時データの丸め込み

> (x <- now())
[1] "2016-05-20 08:13:47 JST"
> floor_date(x, "second")
[1] "2016-05-20 08:13:47 JST"
> floor_date(x, "minute")
[1] "2016-05-20 08:13:00 JST"
> floor_date(x, "day")
[1] "2016-05-20 JST"
> floor_date(x, "year")
[1] "2016-01-01 JST"

force_tz

タイムゾーンの強制変換

> (x <- now(tzone = "Asia/Tokyo"))
[1] "2016-05-20 08:13:47 JST"
> force_tz(x, tzone = "GMT")
[1] "2016-05-20 08:13:47 GMT"

here

ローカルタイムゾーンの現在時刻を表示

> here()
[1] "2016-05-20 08:13:47 JST"

int_overlaps

2つの期間に重なりがあるか確かめる

> int1 <- new_interval(ymd("2001-01-01"), ymd("2002-01-01"))
'new_interval' is deprecated; use 'interval' instead. Deprecated in version '1.5.0'.
> int2 <- new_interval(ymd("2001-06-01"), ymd("2002-06-01"))
'new_interval' is deprecated; use 'interval' instead. Deprecated in version '1.5.0'.
> int_overlaps(int1, int2)
[1] TRUE
> int3 <- new_interval(ymd("2003-01-01"), ymd("2004-01-01"))
'new_interval' is deprecated; use 'interval' instead. Deprecated in version '1.5.0'.
> int_overlaps(int1, int3)
[1] FALSE

leap_year

うるう年か否か

> x <- as.Date("2009-08-02")
> leap_year(x)
[1] FALSE
> leap_year(2009)
[1] FALSE
> leap_year(2008)
[1] TRUE

minutes

> minutes(2)
[1] "2M 0S"

month

> month(today(), label = TRUE, abbr = FALSE)
[1] May
12 Levels: January < February < March < April < May < June < ... < December

new_interval

> dob <- ymd(19891126)
> new_interval(start = dob, end = today())
'new_interval' is deprecated; use 'interval' instead. Deprecated in version '1.5.0'.
[1] 1989-11-26 UTC--2016-05-20 UTC
> new_interval(dob, today(), tzone = "Asia/Tokyo") / years(1)
'new_interval' is deprecated; use 'interval' instead. Deprecated in version '1.5.0'.
Remainder cannot be expressed as fraction of a period.
  Performing %/%.
estimate only: convert periods to intervals for accuracy
[1] 26

now

現在時刻を取得

> now()
[1] "2016-05-20 08:13:47 JST"
> now(tzone = olson_time_zones()[10])
[1] "2016-05-20 01:13:47 CAT"

olson_time_zones

オルソン時間帯の一覧を表示

> str(olson_time_zones())
 chr [1:421] "Africa/Abidjan" "Africa/Accra" ...

origin

協定世界時の世界標準時

> origin
[1] "1970-01-01 UTC"

rollback

前月の最終日にする

> rollback(x)
[1] "2009-07-31"

today

今日の日付

> today()
[1] "2016-05-20"

wday

> wday(today(), label = TRUE, abbr = FALSE)
[1] Friday
7 Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < ... < Saturday

week

> week(today())
[1] 21

ymd

関数名を入れ替えて年月日を表示。y... 年、m... 月, d... 日。

> ymd("20150901")
[1] "2015-09-01"
> dmy("01092015")
[1] "2015-09-01"
> mdy("090115")
[1] "2015-09-01"

ymd_hms

> ymd_hms(c("2013-01-24 19:39:07.880-0600",
+           "2013-01-24 19:39:07.880", "2013-01-24 19:39:07.880-06:00",
+           "2013-01-24 19:39:07.880-06", "2013-01-24 19:39:07.880Z"),
+         tz = "Asia/Tokyo")
Date in ISO8601 format; converting timezone from UTC to "Asia/Tokyo".
Date in ISO8601 format; converting timezone from UTC to "Asia/Tokyo".
Date in ISO8601 format; converting timezone from UTC to "Asia/Tokyo".
[1] "2013-01-25 10:39:07 JST" "2013-01-24 19:39:07 JST"
[3] "2013-01-25 10:39:07 JST" "2013-01-25 10:39:07 JST"
[5] "2013-01-25 04:39:07 JST"