在R中,如果我有这样的时间序列:
datetime value
2014-12-01 00:00 100
2014-12-01 01:00 111
2014-12-01 02:00 98
2014-12-01 03:00 127
2014-12-01 04:00 114
2014-12-01 05:00 105
...
当这个系列的累积达到某个值时,如何获得插值日期时间?像这样:
somefunction(data, 250) // returns 2014-12-01 01:23
答案 0 :(得分:1)
您可以使用approx
:
Data <- structure(list(datetime = structure(c(1417413600, 1417417200,
1417420800, 1417424400, 1417428000, 1417431600), class = c("POSIXct",
"POSIXt"), tzone = ""), value = c(100L, 111L, 98L, 127L, 114L, 105L)),
.Names = c("datetime", "value"), row.names = c(NA, -6L),
class = "data.frame")
itime <- .POSIXct(approx(cumsum(Data$value), Data$datetime, 250)$y,
tz=attr(Data$datetime, "tzone"))
答案 1 :(得分:1)
首先,确保您的datetime
是一个不错的POSIXct
对象,如下所示:
foo <- structure(list(datetime = structure(c(1417388400, 1417392000,
1417395600, 1417399200, 1417402800, 1417406400), class = c("POSIXct",
"POSIXt"), tzone = ""), value = c(100, 111, 98, 127, 114, 105
)), .Names = c("datetime", "value"), row.names = c(NA, -6L), class = "data.frame")
foo
datetime value
1 2014-12-01 00:00:00 100
2 2014-12-01 01:00:00 111
3 2014-12-01 02:00:00 98
4 2014-12-01 03:00:00 127
5 2014-12-01 04:00:00 114
6 2014-12-01 05:00:00 105
然后,approxfun
是你的朋友:
ff <- with(foo,approxfun(x=cumsum(value),y=datetime))
as.POSIXct(ff(250),origin=as.POSIXct("1970-01-01 01:00:00"))
[1] "2014-12-01 01:23:52 CET"