当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R lubridate duration 创建一个持续时间对象。


duration() 创建具有指定值的持续时间对象。不同单位的条目是累积的。持续时间显示为时间跨度中的秒数。当此数字较大时,持续时间也会以较大单位显示估计值,但是底层对象始终记录为固定的秒数。出于显示和创建目的,单位使用最常见的长度(以秒为单位)转换为秒。分钟 = 60 秒,小时 = 3600 秒,天 = 86400 秒,周 = 604800。由于其可变性,不使用大于周的单位。

用法

duration(num = NULL, units = "seconds", ...)

dseconds(x = 1)

dminutes(x = 1)

dhours(x = 1)

ddays(x = 1)

dweeks(x = 1)

dmonths(x = 1)

dyears(x = 1)

dmilliseconds(x = 1)

dmicroseconds(x = 1)

dnanoseconds(x = 1)

dpicoseconds(x = 1)

is.duration(x)

参数

num

时间单位的数字或字符向量。在字符串表示中,支持所有明确的名称单位和缩写以及 ISO 8601 格式; 'm' 代表月份,'M' 代表分钟,除非存在 ISO 8601 "P" 修饰符(请参阅示例)。支持分数单位。

units

指定 num 引用的单位类型的字符串。当num 为字符时,该参数被忽略。

...

要包含在持续时间及其数量中的时间单位列表。支持秒、分钟、小时、天、周、月和年。月份和年份的持续时间假定一年由 365.25 天组成。

x

持续时间中包含的单位数的数值。

持续时间对象

细节

持续时间记录时间跨度内的确切秒数。它们测量准确的时间流逝,但并不总是与以较大时间单位(例如小时、月和年)进行的测量保持一致。这是因为较大时间单位的长度可能会受到闰年和夏令时等惯例的影响。 Base R 提供了第二个类来测量持续时间,即 difftime 类。

可以使用辅助函数 dweeks()ddays()dminutes()dseconds() 轻松创建 Duration 对象。这些对象可以与日期时间相加或相减,以创建类似于面向对象编程的用户接口。

也可以看看

例子


### Separate period and units vectors

duration(90, "seconds")
#> [1] "90s (~1.5 minutes)"
duration(1.5, "minutes")
#> [1] "90s (~1.5 minutes)"
duration(-1, "days")
#> [1] "-86400s (~-1 days)"

### Units as arguments

duration(day = -1)
#> [1] "-86400s (~-1 days)"
duration(second = 90)
#> [1] "90s (~1.5 minutes)"
duration(minute = 1.5)
#> [1] "90s (~1.5 minutes)"
duration(mins = 1.5)
#> [1] "90s (~1.5 minutes)"
duration(second = 3, minute = 1.5, hour = 2, day = 6, week = 1)
#> [1] "1130493s (~1.87 weeks)"
duration(hour = 1, minute = -60)
#> [1] "0s"

### Parsing

duration("2M 1sec")
#> [1] "121s (~2.02 minutes)"
duration("2hours 2minutes 1second")
#> [1] "7321s (~2.03 hours)"
duration("2d 2H 2M 2S")
#> [1] "180122s (~2.08 days)"
duration("2days 2hours 2mins 2secs")
#> [1] "180122s (~2.08 days)"
# Missing numerals default to 1. Repeated units are added up.
duration("day day")
#> [1] "172800s (~2 days)"

### ISO 8601 parsing

duration("P3Y6M4DT12H30M5S")
#> [1] "110842205s (~3.51 years)"
duration("P23DT23H") # M stands for months
#> [1] "2070000s (~3.42 weeks)"
duration("10DT10M") # M stands for minutes
#> [1] "864600s (~1.43 weeks)"
duration("P23DT60H 20min 100 sec") # mixing ISO and lubridate style parsing
#> [1] "2204500s (~3.65 weeks)"

# Comparison with characters (from v1.6.0)

duration("day 2 sec") > "day 1sec"
#> [1] TRUE


## ELEMENTARY CONSTRUCTORS:

dseconds(1)
#> [1] "1s"
dminutes(3.5)
#> [1] "210s (~3.5 minutes)"

x <- ymd("2009-08-03", tz = "America/Chicago")
x + ddays(1) + dhours(6) + dminutes(30)
#> [1] "2009-08-04 06:30:00 CDT"
x + ddays(100) - dhours(8)
#> [1] "2009-11-10 15:00:00 CST"

class(as.Date("2009-08-09") + ddays(1)) # retains Date class
#> [1] "Date"
as.Date("2009-08-09") + dhours(12)
#> [1] "2009-08-09 12:00:00 UTC"
class(as.Date("2009-08-09") + dhours(12))
#> [1] "POSIXct" "POSIXt" 
# converts to POSIXt class to accomodate time units

dweeks(1) - ddays(7)
#> [1] "0s"
c(1:3) * dhours(1)
#> [1] "3600s (~1 hours)"  "7200s (~2 hours)"  "10800s (~3 hours)"

# compare DST handling to durations
boundary <- ymd_hms("2009-03-08 01:59:59", tz = "America/Chicago")
boundary + days(1) # period
#> [1] "2009-03-09 01:59:59 CDT"
boundary + ddays(1) # duration
#> [1] "2009-03-09 02:59:59 CDT"
is.duration(as.Date("2009-08-03")) # FALSE
#> [1] FALSE
is.duration(duration(days = 12.4)) # TRUE
#> [1] TRUE
源代码:R/durations.r

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Create a duration object.。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。