R语言 如何减去时间

R语言 如何减去时间

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

方法1:使用 R语言中difftime() 方法

R语言中的difftime()方法是用来计算给定的时间戳的差异。它被用来返回一个由单位属性伴随的difftime类对象。”difftime “对象只支持有限的算术运算,也就是说,它们可以被加或减,以及与数字向量相乘或相除。这个方法返回的结果是基于第一个参数的时间戳值减去第二个参数,也就是time1-time2。如果时间1大于时间2,结果为正,如果两个时间帧相等,结果为0,其余情况为负。

语法: difftime(time1, time2, tz,units = c(“auto”, “secs”, “minutes”, “hours”, “days”, “weeks”))

参数

  • time1和time2 – 数据时间对象或数字向量
  • tz – 时区(可选)
  • units – 指明要进行算术的单位

返回类型: 一个对日期时间对象进行算术的difftime对象。

例子

# 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")

输出

[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

方法2

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

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"

方法3 :

我们可以在这个对象上使用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"

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程