[英]difftime in R is not calculating correctly
I am trying to find the difference between two dates in hours, and for the time differences that occur over the span of more than one day I am getting really outrageous and incorrect numbers. 我试图找出两个日期之间的时差,以小时为单位,而对于超过一天的时间间隔中出现的时差,我得到的数字实在令人难以置信和错误。
Here is an example of the data: 这是数据示例:
Observation Status DateTime
1 Active 2016-11-04 22:32:49
2 Inactive 2016-11-05 08:30:56
I am running this command: 我正在运行以下命令:
getDiff <- function(x) {
difftime(shift(x, fill = NA, type = "lead"), x, units = "hours")
}
diff_result <- dataframe[, time.diff := ifelse(Status == "Active",
getDiff(DateTime), NA)]
And I get the following output: 我得到以下输出:
Observation Status DateTime Time.diff
1 Active 2016-11-04 22:32:49 8757.884
2 Inactive 2016-11-05 08:30:56
This command works for all other differences that do not happen on separate days. 此命令适用于所有其他在不同日期不会发生的差异。 The correct answer should be around 10 hours, not over 8000.
正确答案应该在10小时左右,而不是超过8000。
Also, 也,
> class(DataFrame$DateTime)
[1] "POSIXct" "POSIXt"
Thank you in advance! 先感谢您!
It seems OP has not converted DateTime
format correctly. 看来OP没有正确转换
DateTime
格式。 The 8757
hours are equivalent to about 1 year. 8757
小时相当于大约1年。 Hence, it is possible DateTime
are wrongly formatted. 因此,很有可能
DateTime
格式错误。
The result looks fine using OP's data at my end. 最后,使用OP的数据,结果看起来不错。
library(data.table)
getDiff <- function(x) {
difftime(shift(x, fill = NA, type = "lead"), x, units = "hours")
}
setDT(df)
diff_result <- df[, time.diff := ifelse(Status == "Active",
getDiff(DateTime), NA)]
diff_result
# Observation Status DateTime time.diff
# 1: 1 Active 2016-11-04 22:32:49 9.968611
# 2: 2 Inactive 2016-11-05 08:30:56 NA
#
Data: 数据:
df <- read.table(text =
"Observation Status DateTime
1 Active '2016-11-04 22:32:49'
2 Inactive '2016-11-05 08:30:56'",
header = TRUE, stringsAsFactors = FALSE)
df$DateTime = as.POSIXct(df$DateTime, format = "%Y-%m-%d %H:%M:%S")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.