对不起,我这里没有可重现的例子。
任何人都可以解释或猜测为什么其中一个差异会在几天内出现,另一个在几秒钟内出现?
> str(dat)
$ Date.Of.Visit : chr "2010-02-11" "2010-05-13" "2012-10-16" "2014-03-24" ...
$ Date.Diagnosed : chr "2003-08-23" "2003-08-23" "2003-08-23" NA ...
$ Date.Of.Birth : chr "1992-01-19" "1992-01-19" "1992-01-19" NA ...
看起来都一样。
dat$Date.Of.Visit <- ymd(dat$Date.Of.Visit)
dat$Date.Diagnosed <- ymd(dat$Date.Diagnosed)
dat$Date.Of.Birth <- ymd(dat$Date.Of.Birth)
全部通过相同的润滑功能。
> class(dat$Date.Of.Visit)
[1] "POSIXct" "POSIXt"
> class(dat$Date.Of.Birth)
[1] "POSIXct" "POSIXt"
> class(dat$Date.Diagnosed)
[1] "POSIXct" "POSIXt"
看起来都一样。
> (dat$Date.Of.Visit - dat$Date.Of.Birth)[1:10]
Time differences in days
[1] 6598 6689 7576 NA 3043 3141 3218 6784 4813 4974
> (dat$Date.Diagnosed - dat$Date.Of.Birth)[1:10]
Time differences in secs
[1] 365817600 365817600 365817600 NA 256003200 256003200 256003200 243302400 194486400 194486400
我唯一能想到的就是Date.Of.Visit已经完成,Date.Of.Birth和Date.Diagnosed有一些缺失数据的位置。
答案 0 :(得分:1)
很难在没有原始数据的情况下最终回答,但请查看difftime
的代码,该代码用于减去POSIXct
个对象:
> difftime
function (time1, time2, tz, units = c("auto", "secs", "mins",
"hours", "days", "weeks"))
{
if (missing(tz)) {
time1 <- as.POSIXct(time1)
time2 <- as.POSIXct(time2)
}
else {
time1 <- as.POSIXct(time1, tz = tz)
time2 <- as.POSIXct(time2, tz = tz)
}
z <- unclass(time1) - unclass(time2)
attr(z, "tzone") <- NULL
units <- match.arg(units)
if (units == "auto") {
if (all(is.na(z)))
units <- "secs"
else {
zz <- min(abs(z), na.rm = TRUE)
if (is.na(zz) || zz < 60)
units <- "secs"
else if (zz < 3600)
units <- "mins"
else if (zz < 86400)
units <- "hours"
else units <- "days"
}
}
switch(units, secs = .difftime(z, units = "secs"), mins = .difftime(z/60,
units = "mins"), hours = .difftime(z/3600, units = "hours"),
days = .difftime(z/86400, units = "days"), weeks = .difftime(z/(7 *
86400), units = "weeks"))
}
因此,如果减法满足any(is.na(z))
或min(abs(z), na.rm = TRUE) < 60
,则由于您未指定units
,因此单位将被强制为秒。例如:
> difftime("2014-03-24", c(NA,NA,NA))
Time differences in secs
[1] NA NA NA
> difftime("2014-03-24", c('2010-01-01',NA,NA))
Time differences in days
[1] 1542.958 NA NA
> difftime("2014-03-24", c('2010-01-01','2014-03-24',NA))
Time differences in secs
[1] 133311600 0 NA
最可能的罪魁祸首是Date.Diagnosed
和Date.Of.Birth
之间的差异不到一分钟 - 根据您在此处粘贴的内容,这两个向量都不包含{{1} } values,排除了NA
开关。