润滑固定时间单位
Lubridate fix the time units
当我们取两个时间的差时,那里会自动发生一些事情。
> ymd_hms("2016-05-09 15:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of -1 secs
> ymd_hms("2016-05-09 16:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of 59.98333 mins
> ymd_hms("2016-05-10 16:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of 1.041655 days
如何在不使用 difftime
函数的情况下修复单位。
所以我可以执行以下操作:
VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
ymd_hms("2016-05-10 17:19:33"),
ymd_hms("2016-05-10 19:55:03")
)
diffs = diff(VECTOR)
IntervalsInHours = toHours(diffs)
此外,有什么方法可以知道 lubridate 时间对象中使用的单位。例如,
> ymd_hms("2016-05-09 15:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of -1 secs
这里使用的单位是seconds
.
请尝试以下将时差转换为小时。
library(lubridate)
x=ymd_hms("2016-05-09 16:17:03")
y=ymd_hms("2016-05-19 15:17:04")
diffs=as.duration(x-y)
IntervalsInHours=as.numeric(abs(diffs))/3600;IntervalsInHours
或者你可以这样使用:
library(lubridate)
x=ymd_hms("2016-05-09 16:17:03")
y=ymd_hms("2016-05-19 16:17:04")
diffs=as.duration(x-y);
IntervalsInHours=abs(diffs)/dhours(1);IntervalsInHours
"you want to use diff function to take the time differences between a VECTOR elements, only in the units specified"
请尝试以下代码:(通过 int_diff 函数)
> VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
+ ymd_hms("2016-05-10 16:17:04"),
+ ymd_hms("2016-05-10 17:19:33"),
+ ymd_hms("2016-05-10 19:55:03")
+ )
> as.numeric(int_diff(VECTOR))
[1] 1 3749 9330
> round(as.numeric(int_diff(VECTOR))/3600,2)
[1] 0.00 1.04 2.59
看,无论时间间隔最小单位是秒还是非秒,它总是按秒缩放,如下所示。
> VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
+ #ymd_hms("2016-05-10 16:17:04"),
+ ymd_hms("2016-05-10 17:19:33"),
+ ymd_hms("2016-05-10 19:55:03")
+ )
> as.numeric(int_diff(VECTOR))
[1] 3750 9330
> round(as.numeric(int_diff(VECTOR))/3600,2)
[1] 1.04 2.59
我写了两个函数以防有人觉得有用。
timeDiffUnitConvert = function(Diffs, to="day", roundingN = 1){
if(to == "day"){
R = round(as.numeric(as.duration(Diffs))/3600/24,roundingN)
} else if (to == "hour") {
R = round(as.numeric(as.duration(Diffs))/3600, roundingN)
} else if (to == "min") {
R = round(as.numeric(as.duration(Diffs))/60, roundingN)
} else if (to == "sec"){
R = round(as.numeric(as.duration(Diffs)), roundingN)
} else {
stop("to which unit? it must be `day`, `hour`, `min` or `sec`.")
}
R
}
timeDiffVector = function(TimeVector, to="day", roundingN = 1, attachNaMode = "none"){
R = timeDiffUnitConvert(diff(TimeVector), to = to, roundingN = roundingN)
if(attachNaMode == "leading"){
R = c(NA,R)
} else if(attachNaMode == "trailing"){
R = c(R,NA)
} else{
stop("check your attachNaMode: shall be either `leading` or `trailing`")
}
R
}
当我们取两个时间的差时,那里会自动发生一些事情。
> ymd_hms("2016-05-09 15:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of -1 secs
> ymd_hms("2016-05-09 16:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of 59.98333 mins
> ymd_hms("2016-05-10 16:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of 1.041655 days
如何在不使用 difftime
函数的情况下修复单位。
所以我可以执行以下操作:
VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
ymd_hms("2016-05-10 17:19:33"),
ymd_hms("2016-05-10 19:55:03")
)
diffs = diff(VECTOR)
IntervalsInHours = toHours(diffs)
此外,有什么方法可以知道 lubridate 时间对象中使用的单位。例如,
> ymd_hms("2016-05-09 15:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of -1 secs
这里使用的单位是seconds
.
请尝试以下将时差转换为小时。
library(lubridate)
x=ymd_hms("2016-05-09 16:17:03")
y=ymd_hms("2016-05-19 15:17:04")
diffs=as.duration(x-y)
IntervalsInHours=as.numeric(abs(diffs))/3600;IntervalsInHours
或者你可以这样使用:
library(lubridate)
x=ymd_hms("2016-05-09 16:17:03")
y=ymd_hms("2016-05-19 16:17:04")
diffs=as.duration(x-y);
IntervalsInHours=abs(diffs)/dhours(1);IntervalsInHours
"you want to use diff function to take the time differences between a VECTOR elements, only in the units specified"
请尝试以下代码:(通过 int_diff 函数)
> VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
+ ymd_hms("2016-05-10 16:17:04"),
+ ymd_hms("2016-05-10 17:19:33"),
+ ymd_hms("2016-05-10 19:55:03")
+ )
> as.numeric(int_diff(VECTOR))
[1] 1 3749 9330
> round(as.numeric(int_diff(VECTOR))/3600,2)
[1] 0.00 1.04 2.59
看,无论时间间隔最小单位是秒还是非秒,它总是按秒缩放,如下所示。
> VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
+ #ymd_hms("2016-05-10 16:17:04"),
+ ymd_hms("2016-05-10 17:19:33"),
+ ymd_hms("2016-05-10 19:55:03")
+ )
> as.numeric(int_diff(VECTOR))
[1] 3750 9330
> round(as.numeric(int_diff(VECTOR))/3600,2)
[1] 1.04 2.59
我写了两个函数以防有人觉得有用。
timeDiffUnitConvert = function(Diffs, to="day", roundingN = 1){
if(to == "day"){
R = round(as.numeric(as.duration(Diffs))/3600/24,roundingN)
} else if (to == "hour") {
R = round(as.numeric(as.duration(Diffs))/3600, roundingN)
} else if (to == "min") {
R = round(as.numeric(as.duration(Diffs))/60, roundingN)
} else if (to == "sec"){
R = round(as.numeric(as.duration(Diffs)), roundingN)
} else {
stop("to which unit? it must be `day`, `hour`, `min` or `sec`.")
}
R
}
timeDiffVector = function(TimeVector, to="day", roundingN = 1, attachNaMode = "none"){
R = timeDiffUnitConvert(diff(TimeVector), to = to, roundingN = roundingN)
if(attachNaMode == "leading"){
R = c(NA,R)
} else if(attachNaMode == "trailing"){
R = c(R,NA)
} else{
stop("check your attachNaMode: shall be either `leading` or `trailing`")
}
R
}