我从我的GPS追踪器导入了数据,我试图弄清楚如何最好地计算在给定时间(例如12分钟)内运行的最远距离或者给定禁用的最佳时间(例如5英里)。鉴于观察是以不等的间隔进行的,而且我的速度也不恒定,我将得到如下表所示的数据:
x <- read.table(header=T, sep="", stringsAsFactors = FALSE,text="
time dist
4 3
5 4
5 6
3 2
5 5
4 5
4 3
4 2
5 6")
到目前为止,我最好的尝试是生成新的数据集,其中时间以一个单位为单位。然后,在给定时间内计算最远距离相对容易。这样做的缺点是:a)我需要重复相同的逻辑以获得最佳时间(使用单位距离生成数据),b)对于具有数千个数据点的数据,它似乎是非常次优的解决方案。
# Generate data frame where each row represents one unit of time
z_adj <- data.frame(
time = unlist(sapply(x$time, function(s) rep(s, each = s))),
dist = unlist(sapply(seq_along(x$dist), function(s) rep(x$dist[s], each = x$time[s])))
)
z_adj$seq_time <- seq_along(z_adj$time)
z_adj$time_dist <- z_adj$dist / z_adj$time
# Furthest distance given time
# Time 10
z_adj$in_t10 <- sapply(z_adj$seq_time, function(s) sum(z_adj$dist[s:(s+9)]))
z_adj$in_t10[which(z_adj$in_t10 == max(z_adj$in_t10, na.rm = T))]
# Fastest time given distance
# ... would need to do the above again with constant distance :/
有没有更简单的方法来实现这一目标?
答案 0 :(得分:0)
您可以使用以下内容:
x <- read.table(header=T, sep="", stringsAsFactors = FALSE,text="
time dist
4 3
5 4
5 6
3 2
5 5
4 5
4 3
4 2
5 6")
# Add starting point and cumulatice time/distance
x <- rbind(c(0,0), x)
x$total_time <- cumsum(x$time)
x$total_dist <- cumsum(x$dist)
# function to interpolate and calculate lagging differences
foo <- function(x, y, n) {
interpolation <- approx(x, y, xout = seq(min(x), max(x)))
diff(interpolation$y, lag = n)
}
# Max distance in ten units of time
max(foo(x$total_time, x$total_dist, 10))
# Min time for ten units of distance
min(foo(x$total_dist, x$total_time, 10))
顺便说一句,在您的代码中,您应该总结z_adj$time_dist
而不是z_adj$dist
来获得正确的结果。