时间值

时间:2016-06-03 08:41:10

标签: r time-series interpolation spline

一列有时间和日期值,另一列有速度值,我想插入任何给定时间的速度值,如下所示,但我收到错误。

这是数据集。

 startTimestamp        avg_Speed
 2016-04-11 00:01:07   74.45
 2016-04-11 00:05:10   73.58
 2016-04-11 00:06:09   89.90 

我想在 - “00:03:11”插入速度值,我只是使用了样条函数,

newspeed <- splinefun(k$startTimestamp, k$avg_Speed, method = "monoH.FC")
newspeed("00:03:11")

这就是我得到的错误。

  

if(extrapol ==“linear”&amp;&amp; any(iXtra&lt; - (iL&lt; - (i == 0))|   (iR ......

其他一些信息:class(f$startTimestamp) = "POSIXct" "POSIXt"

1 个答案:

答案 0 :(得分:0)

我认为它可以轻松解决您的问题,将类更改为数字(分钟)。

# making data (you needn't)
k <- data.frame(startTimestamp = c("2016-04-11 00:01:07", "2016-04-11 00:05:10", "2016-04-11 00:06:09"),
                avg_Speed = c(74.45, 73.58, 89.90))
k[,1] <- as.POSIXct(k[,1], "GMT")

# changing class ( POSIXct -> difftime -> numeric )
zero_time <- as.POSIXct("2016-04-11 00:00:00", "GMT") # using your time zone
k$difftime <- as.numeric(difftime(k$startTimestamp, zero_time))
class(k$difftime)  ## "numeric" (min)

# interpolate
newspeed <- splinefun(k$difftime, k$avg_Speed, method = "monoH.FC")
newspeed( 3 + 11/60 )   # or newspeed( as.numeric(as.difftime("00:03:11")) )
          ## results [1] 69.66494