将带时间戳的数据与另一个数据集中最接近的时间相匹配。正确矢量化?更快的方法?
Matching timestamped data to closest time in another dataset. Properly vectorized? Faster way?
我试图将一个数据帧中的时间戳与第二个数据帧中最接近的时间戳相匹配,以便从第二个数据帧中提取数据。有关我的方法的一般示例,请参见下文:
library(lubridate)
data <- data.frame(datetime=ymd_hms(c('2015-04-01 12:23:00 UTC', '2015-04-01 13:49:00 UTC', '2015-04-01 14:06:00 UTC' ,'2015-04-01 14:49:00 UTC')),
value=c(1,2,3,4))
reference <- data.frame(datetime=ymd_hms(c('2015-04-01 12:00:00 UTC', '2015-04-01 13:00:00 UTC', '2015-04-01 14:00:00 UTC' ,'2015-04-01 15:00:00 UTC', '2015-04-01 16:00:00 UTC')),
refvalue=c(5,6,7,8,9))
data$refvalue <- apply(data, 1, function (x){
differences <- abs(as.numeric(difftime(ymd_hms(x['datetime']), reference$datetime)))
mindiff <- min(differences)
return(reference$refvalue[differences == mindiff])
})
data
# datetime value refvalue
# 1 2015-04-01 12:23:00 1 5
# 2 2015-04-01 13:49:00 2 7
# 3 2015-04-01 14:06:00 3 7
# 4 2015-04-01 14:49:00 4 8
这工作正常,但速度很慢,因为在我的实际应用程序中参考数据帧非常大。此代码是否已正确矢量化?是否有更快、更优雅的方式来执行此操作?
您可以使用 "nearest" 选项尝试 data.table
s 滚动连接
library(data.table) # v1.9.6+
setDT(reference)[data, refvalue, roll = "nearest", on = "datetime"]
# [1] 5 7 7 8
我想知道这是否能够匹配 data.table 解决方案的速度,但它是一个 base-R 矢量化解决方案,应该优于您的 apply
版本。而且由于它实际上从未计算过距离,因此它实际上可能比 data.table 最近的方法更快。这会将间隔中点的长度添加到可能的最低值或间隔的起点以创建一组 "mid-breaks",然后使用 findInterval
函数来处理时间。这会在 reference
数据集的行中创建一个合适的索引,然后 "refvalue" 可以 "transferred" 到 data
对象。
data$reefvalue <- reference$refvalue[
findInterval( data$datetime,
c(-Inf, head(reference$datetime,-1))+
c(0, diff(as.numeric(reference$datetime))/2 )) ]
# values are [1] 5 7 7 8
我试图将一个数据帧中的时间戳与第二个数据帧中最接近的时间戳相匹配,以便从第二个数据帧中提取数据。有关我的方法的一般示例,请参见下文:
library(lubridate)
data <- data.frame(datetime=ymd_hms(c('2015-04-01 12:23:00 UTC', '2015-04-01 13:49:00 UTC', '2015-04-01 14:06:00 UTC' ,'2015-04-01 14:49:00 UTC')),
value=c(1,2,3,4))
reference <- data.frame(datetime=ymd_hms(c('2015-04-01 12:00:00 UTC', '2015-04-01 13:00:00 UTC', '2015-04-01 14:00:00 UTC' ,'2015-04-01 15:00:00 UTC', '2015-04-01 16:00:00 UTC')),
refvalue=c(5,6,7,8,9))
data$refvalue <- apply(data, 1, function (x){
differences <- abs(as.numeric(difftime(ymd_hms(x['datetime']), reference$datetime)))
mindiff <- min(differences)
return(reference$refvalue[differences == mindiff])
})
data
# datetime value refvalue
# 1 2015-04-01 12:23:00 1 5
# 2 2015-04-01 13:49:00 2 7
# 3 2015-04-01 14:06:00 3 7
# 4 2015-04-01 14:49:00 4 8
这工作正常,但速度很慢,因为在我的实际应用程序中参考数据帧非常大。此代码是否已正确矢量化?是否有更快、更优雅的方式来执行此操作?
您可以使用 "nearest" 选项尝试 data.table
s 滚动连接
library(data.table) # v1.9.6+
setDT(reference)[data, refvalue, roll = "nearest", on = "datetime"]
# [1] 5 7 7 8
我想知道这是否能够匹配 data.table 解决方案的速度,但它是一个 base-R 矢量化解决方案,应该优于您的 apply
版本。而且由于它实际上从未计算过距离,因此它实际上可能比 data.table 最近的方法更快。这会将间隔中点的长度添加到可能的最低值或间隔的起点以创建一组 "mid-breaks",然后使用 findInterval
函数来处理时间。这会在 reference
数据集的行中创建一个合适的索引,然后 "refvalue" 可以 "transferred" 到 data
对象。
data$reefvalue <- reference$refvalue[
findInterval( data$datetime,
c(-Inf, head(reference$datetime,-1))+
c(0, diff(as.numeric(reference$datetime))/2 )) ]
# values are [1] 5 7 7 8