當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R lubridate interval 用於創建和操作 Interval 對象的實用程序

interval() 創建具有指定開始日期和結束日期的 Interval 對象。如果開始日期早於結束日期,則間隔將為正值。否則,它將是負麵的。從 v1.7.2 開始支持 ISO 8601 格式的字符向量。

int_start() /int_end()int_start<-() /int_end<-() 分別是間隔的開始/結束日期的"accessors" 和"setters"。

int_flip() 顛倒間隔中開始日期和結束日期的順序。新間隔發生在與原始間隔相同的時間跨度內,但方向相反。

int_shift() 將時間間隔的開始日期和結束日期在時間軸上移動指定的量。請注意,如果間隔由 period 對象移動,則這可能會更改間隔的確切長度。由 Duration 或 difftime 對象移動的間隔將保留其精確的長度(以秒為單位)。

int_overlaps() 測試兩個間隔是否重疊。

int_standardize() 確保區間對象中的所有區間均為正數。如果間隔不為正,則翻轉它,使其保留其端點但變為正。

int_aligns() 測試兩個間隔是否共享一個端點。每個間隔的方向被忽略。 int_align測試每個間隔的最早或最晚時刻是否同時出現。

int_diff() 返回日期時間向量的元素之間出現的間隔。 int_diff()diff() 的 POSIXt 和 Date 方法類似,但返回 Interval 對象而不是 difftime 對象。

用法

interval(start = NULL, end = NULL, tzone = tz(start))

start %--% end

is.interval(x)

int_start(int)

int_start(int) <- value

int_end(int)

int_end(int) <- value

int_length(int)

int_flip(int)

int_shift(int, by)

int_overlaps(int1, int2)

int_standardize(int)

int_aligns(int1, int2)

int_diff(times)

參數

start, end

POSIXt、日期或字符向量。當 start 是字符向量且結尾為 NULL 時,假定采用 ISO 8601 規範,但對日期和周期進行更寬鬆的 lubridate 樣式解析(請參閱示例)。

tzone

顯示時間間隔的公認時區

x

一個 R 對象

int

區間對象

value

間隔的開始/結束分配給int

by

要移動的周期或持續時間對象(對於 int_shift )

int1

一個 Interval 對象(對於 int_overlaps()int_aligns() )

int2

一個 Interval 對象(對於 int_overlaps()int_aligns() )

times

POSIXct、POSIXlt 或 Date 類日期時間的向量(對於 int_diff() )

interval() -- Interval 對象。

int_start()int_end() 在用作訪問器時返回 POSIXct 日期對象。用作二傳手時什麽也沒有。

int_length() -- 間隔的數字長度(以秒為單位)。負數表示負區間。

int_flip() -- 翻轉區間對象

int_shift() -- 一個 Interval 對象

int_overlaps() -- 邏輯,如果 int1 和 int2 重疊至少一秒,則為 TRUE。否則為假

int_aligns() -- 邏輯,如果 int1 和 int2 在同一時刻開始或結束,則為 TRUE。否則為假

int_diff() -- 包含 n 個日期時間之間的 n-1 個間隔的間隔對象

細節

間隔是受兩個實際日期時間約束的時間跨度。使用 as.period()as.duration() 可以將間隔準確地轉換為周期或持續時間對象。由於間隔錨定到固定的時間曆史記錄,因此可以計算經過的確切秒數以及間隔期間發生的可變長度時間單位的數量。

也可以看看

例子

interval(ymd(20090201), ymd(20090101))
#> [1] 2009-02-01 UTC--2009-01-01 UTC

date1 <- ymd_hms("2009-03-08 01:59:59")
date2 <- ymd_hms("2000-02-29 12:00:00")
interval(date2, date1)
#> [1] 2000-02-29 12:00:00 UTC--2009-03-08 01:59:59 UTC
interval(date1, date2)
#> [1] 2009-03-08 01:59:59 UTC--2000-02-29 12:00:00 UTC
span <- interval(ymd(20090101), ymd(20090201))

### ISO Intervals

interval("2007-03-01T13:00:00Z/2008-05-11T15:30:00Z")
#> [1] 2007-03-01 13:00:00 UTC--2008-05-11 15:30:00 UTC
interval("2007-03-01T13:00:00Z/P1Y2M10DT2H30M")
#> [1] 2007-03-01 13:00:00 UTC--2008-05-11 15:30:00 UTC
interval("P1Y2M10DT2H30M/2008-05-11T15:30:00Z")
#> [1] 2007-03-01 13:00:00 UTC--2008-05-11 15:30:00 UTC
interval("2008-05-11/P2H30M")
#> [1] 2008-05-11 UTC--2010-11-11 02:00:00 UTC

### More permisive parsing (as long as there are no intermittent / characters)
interval("2008 05 11/P2hours 30minutes")
#> [1] 2008-05-11 UTC--2008-05-11 02:30:00 UTC
interval("08 05 11/P 2h 30m")
#> [1] 2008-05-11 UTC--2010-11-11 02:00:00 UTC

is.interval(period(months = 1, days = 15)) # FALSE
#> [1] FALSE
is.interval(interval(ymd(20090801), ymd(20090809))) # TRUE
#> [1] TRUE
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_start(int)
#> [1] "2001-01-01 UTC"
int_start(int) <- ymd("2001-06-01")
int
#> [1] 2001-06-01 UTC--2002-01-01 UTC

int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_end(int)
#> [1] "2002-01-01 UTC"
int_end(int) <- ymd("2002-06-01")
int
#> [1] 2001-01-01 UTC--2002-06-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_length(int)
#> [1] 31536000
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_flip(int)
#> [1] 2002-01-01 UTC--2001-01-01 UTC
int <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int_shift(int, duration(days = 11))
#> [1] 2001-01-12 UTC--2002-01-12 UTC
int_shift(int, duration(hours = -1))
#> [1] 2000-12-31 23:00:00 UTC--2001-12-31 23:00:00 UTC
int1 <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int2 <- interval(ymd("2001-06-01"), ymd("2002-06-01"))
int3 <- interval(ymd("2003-01-01"), ymd("2004-01-01"))

int_overlaps(int1, int2) # TRUE
#> [1] TRUE
int_overlaps(int1, int3) # FALSE
#> [1] FALSE
int <- interval(ymd("2002-01-01"), ymd("2001-01-01"))
int_standardize(int)
#> [1] 2001-01-01 UTC--2002-01-01 UTC
int1 <- interval(ymd("2001-01-01"), ymd("2002-01-01"))
int2 <- interval(ymd("2001-06-01"), ymd("2002-01-01"))
int3 <- interval(ymd("2003-01-01"), ymd("2004-01-01"))

int_aligns(int1, int2) # TRUE
#> [1] TRUE
int_aligns(int1, int3) # FALSE
#> [1] FALSE
dates <- now() + days(1:10)
int_diff(dates)
#> [1] 2023-02-15 11:55:03 UTC--2023-02-16 11:55:03 UTC
#> [2] 2023-02-16 11:55:03 UTC--2023-02-17 11:55:03 UTC
#> [3] 2023-02-17 11:55:03 UTC--2023-02-18 11:55:03 UTC
#> [4] 2023-02-18 11:55:03 UTC--2023-02-19 11:55:03 UTC
#> [5] 2023-02-19 11:55:03 UTC--2023-02-20 11:55:03 UTC
#> [6] 2023-02-20 11:55:03 UTC--2023-02-21 11:55:03 UTC
#> [7] 2023-02-21 11:55:03 UTC--2023-02-22 11:55:03 UTC
#> [8] 2023-02-22 11:55:03 UTC--2023-02-23 11:55:03 UTC
#> [9] 2023-02-23 11:55:03 UTC--2023-02-24 11:55:03 UTC
源代碼:R/intervals.r

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Utilities for creation and manipulation of Interval objects。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。