【发布时间】:2013-01-31 13:36:24
【问题描述】:
我在 R 中有一个 50 列 x 250 万行的数据框,代表一个时间序列。时间列属于 POSIXct 类。为了进行分析,我需要反复查找给定类在特定时间的系统状态。
我目前的方法如下(简化且可重现):
set.seed(1)
N <- 10000
.time <- sort(sample(1:(100*N),N))
class(.time) <- c("POSIXct", "POSIXt")
df <- data.frame(
time=.time,
distance1=sort(sample(1:(100*N),N)),
distance2=sort(sample(1:(100*N),N)),
letter=sample(letters,N,replace=TRUE)
)
# state search function
time.state <- function(df,searchtime,searchclass){
# find all rows in between the searchtime and a while (here 10k seconds)
# before that
rows <- which(findInterval(df$time,c(searchtime-10000,searchtime))==1)
# find the latest state of the given class within the search interval
return(rev(rows)[match(T,rev(df[rows,"letter"]==searchclass))])
}
# evaluate the function to retrieve the latest known state of the system
# at time 500,000.
df[time.state(df,500000,"a"),]
但是,拨打which 的成本很高。或者,我可以先按班级筛选,然后找到时间,但这并不会改变评估时间。根据 Rprof 的说法,大部分时间是 which 和 ==。
有没有更有效的解决方案?时间点按弱递增排序。
【问题讨论】:
-
我认为这已经很有效了。其中和 == 的复杂度是线性的。
-
拥有 1M 行和 1000 个唯一的
letter值,这仅需要在我的系统上查看 ms。为什么需要优化它? -
对于 250 万行(与行数成线性关系),调用 findInterval 大约需要 70 毫秒。我必须调用这个函数 100k-1M 次,这使得任何优化都非常受欢迎。如果你说没有,我会开始寻找解决方法。
-
我设法找到了一个很好的解决方法,请参阅我的答案。
标签: r optimization dataframe posixct