如何比较R中的时间?
R 编程语言支持使用各种不同格式和说明符的日期和日期时间对象。内置框架as.Date函数单独负责日期的处理,R Programming中的库chron同时处理日期和时间,不支持时区;而 POSIXct 和 POSIXlt 类提供对处理日期时间对象和时区的支持。可以将日期时间对象轻松转换为其他与日期相关的对象。
日期字符串可以首先转换为 POSIXct 对象,然后可以轻松地对其执行基本算术。 POSIXct 对象简化了数学运算的过程,因为它们依赖秒作为时间管理的主要单位。日期将转换为标准时区 UTC。可以使用 R 中的 as.POSIXct(date) 方法将字符串类型的日期对象转换为 POSIXct 对象。由于日期以秒为单位存储,因此可以通过首先将小时和分钟转换为秒单位来执行减法也。
1 hour = 1 * 60 * 60 seconds
1 min = 1 * 60 seconds
方法一:使用逻辑运算符
POSIXct 日期可以使用逻辑运算符进行比较以比较日期。在不同的参数日期之间应用逻辑运算符,并根据输出返回布尔值 TRUE 或 FALSE。
例子:
R
# declaring a time object
time1 <- as.POSIXct("08:32:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("08:32:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
if(time1< time2){
print ("Time1 smaller")
}else{
print ("Time2 smaller")
}
}
R
# declaring a time object
time1 <- as.POSIXct("08:35:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("08:32:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
# checking if time1 is smaller than time2
if(time1< time2){
print ("Time2 - Time1")
# calculating time2-time1
difftime(time2,time1, units = "hours")
}else{
# calculating time1-time2
print ("Time1 - Time2")
difftime(time1,time2, units = "hours")
}
}
R
# declaring a time object
time1 <- as.POSIXct("09:35:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("09:35:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
# checking if time1 is smaller than time2
if(time1< time2){
print ("Time2 - Time1")
# calculating time2-time1
print (time2 -time1)
}else{
# calculating time1-time2
print ("Time1 - Time2")
print (time1-time2)
}
}
输出
[1] "Time 1"
[1] "2021-05-18 08:32:07 UTC"
[1] "Time 2"
[1] "2021-05-18 08:32:08 UTC"
[1] "Time1 smaller"
方法2:使用比较运算符
- 使用 difftime() 方法
R 语言中的 difftime() 方法用于计算给定时间戳的差异。它用于返回带有单位属性的 difftime 类本身的对象。 “difftime”对象仅支持对它们进行有限的算术运算,也就是说,它们可以加减,乘以或除以数值向量。该方法返回的结果是第一个参数的时间戳值减去第二个参数,即time1-time2。如果 time1 大于 time2,则结果为正,如果两个时间范围相等,则结果为 0,其余情况为负。
Syntax: difftime(time1, time2, tz,units = c(“auto”, “secs”, “mins”, “hours”,”days”, “weeks”))
Parameter :
- time1 and time2 – the datetime objects or numeric vectors
- tz – time zone (optional)
- units – the specification of units to perform arithmetic on
Return type : a difftime object applying the airthmetic on datetime object
例子 :
电阻
# declaring a time object
time1 <- as.POSIXct("08:35:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("08:32:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
# checking if time1 is smaller than time2
if(time1< time2){
print ("Time2 - Time1")
# calculating time2-time1
difftime(time2,time1, units = "hours")
}else{
# calculating time1-time2
print ("Time1 - Time2")
difftime(time1,time2, units = "hours")
}
}
输出
[1] "Time 1"
[1] "2021-05-18 08:35:07 UTC"
[1] "Time 2"
[1] "2021-05-18 08:32:08 UTC"
[1] "Time1 - Time2"
Time difference of 0.04972222 hours
- 使用“-”运算符
可以使用减号运算符来比较日期对象,以从较大的日期中减去较小的日期。返回日期的单位是小时、分钟或秒中的最大值。
例子:
电阻
# declaring a time object
time1 <- as.POSIXct("09:35:07", format = "%H:%M:%S")
print ("Time 1")
print (time1)
time2 <- as.POSIXct("09:35:08", format = "%H:%M:%S")
print ("Time 2")
print (time2)
if ( time1 == time2){
print("Equal times")
}else{
# checking if time1 is smaller than time2
if(time1< time2){
print ("Time2 - Time1")
# calculating time2-time1
print (time2 -time1)
}else{
# calculating time1-time2
print ("Time1 - Time2")
print (time1-time2)
}
}
输出
[1] "Time 1"
[1] "2021-05-18 09:35:07 UTC"
[1] "Time 2"
[1] "2021-05-18 09:35:08 UTC"
[1] "Time2 - Time1"
Time difference of 1 secs