0
我已经从我的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 :/
有没有更直接的方法来实现这个目标?
有关我的答案的任何意见? –