round_date()
接受日期時間對象和時間單位,並將其舍入為指定時間單位的最接近值。對於恰好位於兩個連續單位中間的日期時間的舍入,慣例是向上舍入。請注意,這與 R 的 base::round.POSIXt()
函數的行為一致,但不遵循基本 base::round()
函數的約定,即根據 IEC 60559“四舍五入到偶數位”。
支持舍入到最接近的單位或單位的倍數。支持英語中所有有意義的規範 - 秒、分鍾、分鍾、2 分鍾、3 年等。
還支持舍入到小數秒。請注意,由於 POSIXct 對象的浮點表示形式,四舍五入到小於 1 秒的分數可能會導致較大的精度誤差。請參閱示例。
floor_date()
接受一個日期時間對象並將其向下舍入到指定時間單位的最近邊界。
ceiling_date()
接受一個日期時間對象並將其向上舍入到指定時間單位的最近邊界。
用法
round_date(
x,
unit = "second",
week_start = getOption("lubridate.week.start", 7)
)
floor_date(
x,
unit = "seconds",
week_start = getOption("lubridate.week.start", 7)
)
ceiling_date(
x,
unit = "seconds",
change_on_boundary = NULL,
week_start = getOption("lubridate.week.start", 7)
)
參數
- x
-
日期時間對象的向量
- unit
-
字符串、
Period
對象或日期時間對象。當為單例字符串時,它指定要舍入的時間單位或單位的倍數。有效的基本單位是second
,minute
,hour
,day
,week
,month
,bimonth
,quarter
,season
,halfyear
和year
。period()
構造函數中允許使用任意唯一的英文縮寫。支持四舍五入到單位的倍數(周除外)。當unit
是Period
對象時,使用周期對象的單位。這相當於將 period 對象轉換為其字符串表示形式並作為unit
參數傳遞。當
unit
是日期時間對象時,將四舍五入到unit
中最接近的元素。如果unit
向量的範圍不覆蓋x
的範圍,則ceiling_date()
和floor_date()
舍入到max(x)
和min(x)
中超出range(unit)
的元素。 - week_start
-
周開始日(默認為 7,周日。設置
lubridate.week.start
進行覆蓋)。一周中各天的完整或縮寫名稱可以是英文或由當前區域設置提供。 - change_on_boundary
-
如果這是
NULL
(默認),邊界上的時刻保持不變,但是Date
對象向上舍入到下一個邊界。如果這是TRUE
,邊界上的時刻向上舍入到下一個邊界。如果這是FALSE
,邊界上的任何內容都不會被舍入。這是默認值潤滑之前v1.6.0
。參見部分Rounding Up Date Objects
請參閱下文了解更多詳情。
值
當unit
是字符串時,如果x
是Date並且unit
大於或等於"day",則返回Date對象,否則返回POSIXct對象。當 unit
是日期時間對象時,返回與 unit
相同類和相同時區的日期時間對象。
日期對象的四舍五入
默認情況下,對 Date
對象進行舍入遵循 3 個步驟:
-
轉換為表示日期下限的瞬間:
2000-01-01
-->2000-01-01 00:00:00
-
向上舍入到下一個最接近的舍入單位邊界。例如,如果舍入單位是
month
,則2000-01-01
的下一個最接近邊界是2000-02-01 00:00:00
。這樣做的動機是 "partial"
2000-01-01
在概念上是一個間隔 (2000-01-01 00:00:00
--2000-01-02 00:00:00
) 並且這一天尚未在確切的邊界00:00:00
開始計時。因此,將一天舍入到其下限似乎是錯誤的。可以通過將
change_on_boundary
設置為TRUE
或FALSE
來更改邊界上的行為。 -
如果舍入單位小於一天,則返回步驟 2 中的時刻 (
POSIXct
),否則轉換為並返回Date
對象。
例子
## print fractional seconds
options(digits.secs = 6)
x <- ymd_hms("2009-08-03 12:01:59.23")
round_date(x, ".5s")
#> [1] "2009-08-03 12:01:59 UTC"
round_date(x, "sec")
#> [1] "2009-08-03 12:01:59 UTC"
round_date(x, "second")
#> [1] "2009-08-03 12:01:59 UTC"
round_date(x, "minute")
#> [1] "2009-08-03 12:02:00 UTC"
round_date(x, "5 mins")
#> [1] "2009-08-03 12:00:00 UTC"
round_date(x, "hour")
#> [1] "2009-08-03 12:00:00 UTC"
round_date(x, "2 hours")
#> [1] "2009-08-03 12:00:00 UTC"
round_date(x, "day")
#> [1] "2009-08-04 UTC"
round_date(x, "week")
#> [1] "2009-08-02 UTC"
round_date(x, "month")
#> [1] "2009-08-01 UTC"
round_date(x, "bimonth")
#> [1] "2009-09-01 UTC"
round_date(x, "quarter") == round_date(x, "3 months")
#> [1] TRUE
round_date(x, "halfyear")
#> [1] "2009-07-01 UTC"
round_date(x, "year")
#> [1] "2010-01-01 UTC"
x <- ymd_hms("2009-08-03 12:01:59.23")
floor_date(x, ".1s")
#> [1] "2009-08-03 12:01:59.2 UTC"
floor_date(x, "second")
#> [1] "2009-08-03 12:01:59 UTC"
floor_date(x, "minute")
#> [1] "2009-08-03 12:01:00 UTC"
floor_date(x, "hour")
#> [1] "2009-08-03 12:00:00 UTC"
floor_date(x, "day")
#> [1] "2009-08-03 UTC"
floor_date(x, "week")
#> [1] "2009-08-02 UTC"
floor_date(x, "month")
#> [1] "2009-08-01 UTC"
floor_date(x, "bimonth")
#> [1] "2009-07-01 UTC"
floor_date(x, "quarter")
#> [1] "2009-07-01 UTC"
floor_date(x, "season")
#> [1] "2009-06-01 UTC"
floor_date(x, "halfyear")
#> [1] "2009-07-01 UTC"
floor_date(x, "year")
#> [1] "2009-01-01 UTC"
x <- ymd_hms("2009-08-03 12:01:59.23")
ceiling_date(x, ".1 sec") # imprecise representation at 0.1 sec !!!
#> [1] "2009-08-03 12:01:59.2 UTC"
ceiling_date(x, "second")
#> [1] "2009-08-03 12:02:00 UTC"
ceiling_date(x, "minute")
#> [1] "2009-08-03 12:02:00 UTC"
ceiling_date(x, "5 mins")
#> [1] "2009-08-03 12:05:00 UTC"
ceiling_date(x, "hour")
#> [1] "2009-08-03 13:00:00 UTC"
ceiling_date(x, "day")
#> [1] "2009-08-04 UTC"
ceiling_date(x, "week")
#> [1] "2009-08-09 UTC"
ceiling_date(x, "month")
#> [1] "2009-09-01 UTC"
ceiling_date(x, "bimonth") == ceiling_date(x, "2 months")
#> [1] TRUE
ceiling_date(x, "quarter")
#> [1] "2009-10-01 UTC"
ceiling_date(x, "season")
#> [1] "2009-09-01 UTC"
ceiling_date(x, "halfyear")
#> [1] "2010-01-01 UTC"
ceiling_date(x, "year")
#> [1] "2010-01-01 UTC"
## Period unit argument
floor_date(x, days(2))
#> [1] "2009-08-03 UTC"
floor_date(x, years(1))
#> [1] "2009-01-01 UTC"
## As of R 3.4.2 POSIXct printing of fractional numbers is wrong
as.POSIXct("2009-08-03 12:01:59.3") ## -> "2009-08-03 12:01:59.2 CEST"
#> [1] "2009-08-03 12:01:59.2 UTC"
ceiling_date(x, ".1 sec") ## -> "2009-08-03 12:01:59.2 CEST"
#> [1] "2009-08-03 12:01:59.2 UTC"
## behaviour of `change_on_boundary`
## As per default behaviour `NULL`, instants on the boundary remain the
## same but dates are rounded up
ceiling_date(ymd_hms("2000-01-01 00:00:00"), "month")
#> [1] "2000-01-01 UTC"
ceiling_date(ymd("2000-01-01"), "month")
#> [1] "2000-02-01"
## If `TRUE`, both instants and dates on the boundary are rounded up
ceiling_date(ymd_hms("2000-01-01 00:00:00"), "month", change_on_boundary = TRUE)
#> [1] "2000-02-01 UTC"
ceiling_date(ymd("2000-01-01"), "month")
#> [1] "2000-02-01"
## If `FALSE`, both instants and dates on the boundary remain the same
ceiling_date(ymd_hms("2000-01-01 00:00:00"), "month", change_on_boundary = FALSE)
#> [1] "2000-01-01 UTC"
ceiling_date(ymd("2000-01-01"), "month")
#> [1] "2000-02-01"
x <- ymd_hms("2000-01-01 00:00:00")
ceiling_date(x, "month")
#> [1] "2000-01-01 UTC"
ceiling_date(x, "month", change_on_boundary = TRUE)
#> [1] "2000-02-01 UTC"
## For Date objects first day of the month is not on the
## "boundary". change_on_boundary applies to instants only.
x <- ymd("2000-01-01")
ceiling_date(x, "month")
#> [1] "2000-02-01"
ceiling_date(x, "month", change_on_boundary = TRUE)
#> [1] "2000-02-01"
相關用法
- R lubridate rollbackward 向後或向前滾動上個月、當前或下個月的日期
- R lubridate DateTimeUpdate 更改日期對象的組成部分
- R lubridate stamp 基於人性化模板設置日期和時間格式
- R lubridate interval 用於創建和操作 Interval 對象的實用程序
- R lubridate is.difftime x 是 difftime 對象嗎?
- R lubridate as_date 將對象轉換為日期或日期時間
- R lubridate date 獲取/設置日期時間的日期部分
- R lubridate make_difftime 創建一個 difftime 對象。
- R lubridate is.timespan x 是時間長度嗎?
- R lubridate with_tz 獲取不同時區的日期時間
- R lubridate mplus 在日期中添加和減去月份,但不超過新月份的最後一天
- R lubridate cyclic_encoding 日期時間的循環編碼
- R lubridate as.interval 將對象更改為間隔
- R lubridate second 獲取/設置日期時間的秒部分
- R lubridate quarter 獲取日期時間的財政季度和學期
- R lubridate posix_utils 各種 POSIX 實用程序
- R lubridate date_decimal 將小數轉換為日期
- R lubridate as.duration 將對象更改為持續時間
- R lubridate hour 獲取/設置日期時間的小時部分
- R lubridate minute 獲取/設置日期時間的分鍾部分
- R lubridate month 獲取/設置日期時間的月份部分
- R lubridate duration 創建一個持續時間對象。
- R lubridate leap_year 一年是閏年嗎?
- R lubridate local_time 從日期時間向量獲取當地時間。
- R lubridate make_datetime 從數字表示高效創建日期時間
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Round, floor and ceiling methods for date-time objects。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。