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


R lubridate round_date 日期時間對象的舍入、取整和取整方法

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 , halfyearyearperiod() 構造函數中允許使用任意唯一的英文縮寫。支持四舍五入到單位的倍數(周除外)。當unitPeriod 對象時,使用周期對象的單位。這相當於將 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 相同類和相同時區的日期時間對象。

細節

潤滑,舍入日期時間對象的函數會盡可能保留輸入對象的類。這是通過首先舍入到瞬間,然後按照通常的 R 約定轉換為原始類來完成的。

日期對象的四舍五入

默認情況下,對 Date 對象進行舍入遵循 3 個步驟:

  1. 轉換為表示日期下限的瞬間:2000-01-01 --> 2000-01-01 00:00:00

  2. 向上舍入到下一個最接近的舍入單位邊界。例如,如果舍入單位是 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 設置為 TRUEFALSE 來更改邊界上的行為。

  3. 如果舍入單位小於一天,則返回步驟 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/round.r

相關用法


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