📜  如何在 R 中减去时间?

📅  最后修改于: 2022-05-13 01:54:36.637000             🧑  作者: Mango

如何在 R 中减去时间?

在本文中,我们将讨论如何在 R 编程语言中减去时间。

方法 1:在 R 中使用difftime()方法

R 中的 difftime() 方法用于计算给定时间戳的差异。它用于返回带有单位属性的 difftime 类本身的对象。 “difftime”对象仅支持对它们进行有限的算术运算,也就是说,它们可以加减,乘以或除以数值向量。该方法返回的结果是第一个参数的时间戳值减去第二个参数,即time1-time2。如果 time1 大于 time2,则结果为正,如果两个时间范围相等,则结果为 0,其余情况为负。

例子:

R
# declaring first datetime object
time1 <- "2019-08-25 17:18:24"                
  
# declaring second datetime object
time2 <- "2019-08-30 23:09:24"  
  
# declaring third datetimeobject 
time3 <-  "2019-08-22 23:09:24"
  
# calculating differences using 
# difftime between time1 and time2
print("TIME1 - TIME2 ")
  
# difference in hours
print ("Difference in hours : ")
difftime(time1,time2, units = "hours")
  
# difference in minutes
print ("Difference in minutes : ")
difftime(time1,time2,units = "mins")
  
# difference in seconds
print ("Difference in seconds : ")
difftime(time1, time2 ,units="secs")
print("TIME1 - TIME3 ")
  
# calculating differences using difftime
# between time1 and time3
# difference in hours
print ("Difference in hours : ")
difftime(time1, time3, units = "hours")
  
# difference in minutes
print ("Difference in minutes : ")
difftime(time1, time3, units = "mins")
  
# difference in seconds
print ("Difference in seconds : ")
difftime(time1, time3, units="secs")


R
# declaring string type date
str_date <- "2021-04-01"
  
# converting to posixct date
pos_date <- as.POSIXct(str_date)
  
# printing original date
print ("Original Date : ")
print (pos_date)
  
# subtracting 15 seconds 
date <- pos_date - 15
print ("New Date")
print (date)


R
# declaring string type date
str_date <- "2021-04-01"
  
# converting to posixct date
pos_date <- as.POSIXct(str_date)
  
# printing original date
print ("Original Date : ")
print (pos_date)
  
# subtracting 15 seconds 
secs = 15
  
# 25 minutes 
mins = 25 * 60
  
# 3 hr 
hrs = 3 * 60 * 60
date <- pos_date - (secs + mins + hrs)
print ("New Date")
print (date)


R
# declaring string type date
str_date <- "2021-04-01"
  
# converting to posixct date
pos_date <- as.POSIXct(str_date)
  
# printing original date
print ("Original Date : ")
print (pos_date)
  
# subtracting 15 seconds 
secs = 15
  
# 25 minutes 
mins = 28
  
# 3 hr 
hrs = 8
date <- pos_date - as.difftime(secs,units="secs") 
- as.difftime(mins,units="mins") 
- as.difftime(hrs,units="hours")
  
print ("New Date")
print (date)


输出:

[1] "TIME1 - TIME2 "
[1] "Difference in hours : "
Time difference of -125.85 hours
[1] "Difference in minutes : "
Time difference of -7551 mins
[1] "Difference in seconds : "
Time difference of -453060 secs
[1] "TIME1 - TIME3 "
[1] "Difference in hours : "
Time difference of 66.15 hours
[1] "Difference in minutes : "
Time difference of 3969 mins
[1] "Difference in seconds : "
Time difference of 238140 secs

方法二:

日期字符串可以首先转换为POSIXct对象,然后可以轻松地对其执行基本算术。 POSIXct 对象简化了数学运算的过程,因为它们依赖秒作为时间管理的主要单位。日期将转换为标准时区 UTC。可以使用 R 中的 as.POSIXct(date) 方法将字符串类型的日期对象转换为 POSIXct 对象。由于日期以秒为单位存储,因此可以通过先将小时和分钟转换为秒单位来执行减法也。

1 hour = 1 * 60 * 60 seconds
1 min = 1 * 60 seconds

示例 1:

电阻



# declaring string type date
str_date <- "2021-04-01"
  
# converting to posixct date
pos_date <- as.POSIXct(str_date)
  
# printing original date
print ("Original Date : ")
print (pos_date)
  
# subtracting 15 seconds 
date <- pos_date - 15
print ("New Date")
print (date)

输出:

[1] "Original Date : "
[1] "2021-04-01 UTC"
[1] "New Date"
[1] "2021-03-31 23:59:45 UTC"

示例 2:

电阻

# declaring string type date
str_date <- "2021-04-01"
  
# converting to posixct date
pos_date <- as.POSIXct(str_date)
  
# printing original date
print ("Original Date : ")
print (pos_date)
  
# subtracting 15 seconds 
secs = 15
  
# 25 minutes 
mins = 25 * 60
  
# 3 hr 
hrs = 3 * 60 * 60
date <- pos_date - (secs + mins + hrs)
print ("New Date")
print (date)

输出:

[1] "Original Date : "
[1] "2021-04-01 UTC"
[1] "New Date"
[1] "2021-03-31 20:34:45 UTC"

方法三:

相反,我们可以对这个对象使用 as.difftime() 方法,而不是直接对 POSIXct 日期执行算术,从可用日期中减去时间戳。 as.difftime() 方法具有以下语法:

as.difftime(int , units = c("secs","hours","mins")

例子:

电阻

# declaring string type date
str_date <- "2021-04-01"
  
# converting to posixct date
pos_date <- as.POSIXct(str_date)
  
# printing original date
print ("Original Date : ")
print (pos_date)
  
# subtracting 15 seconds 
secs = 15
  
# 25 minutes 
mins = 28
  
# 3 hr 
hrs = 8
date <- pos_date - as.difftime(secs,units="secs") 
- as.difftime(mins,units="mins") 
- as.difftime(hrs,units="hours")
  
print ("New Date")
print (date)

输出

[1] "Original Date : "
[1] "2021-04-01 UTC"
[1] "New Date"
[1] "2021-03-31 15:31:45 UTC"