Finding average of values in the past 2 minutes in a data.table

蹲街弑〆低调 提交于 2019-12-04 21:23:40

Will this do

ts[,avg] <- ts[,val] - 0.5

As logically and seeing your expected result, it is doing the same thing. You can edit you expected result to make it more flexible if I interpreted it wrong.

EDIT:

This base R approach should do the trick. As I an not familiar with manipulating time, I am assuming that arithmetic works in the same way as it does in most of the languages

interval <- minutes(2) #Assuming this is how we define 5 minutes

x$avg <- apply( x, 1, function(y){
               mean(x$value[x$time > ( y["time"]) - interval ) && x$time < y["time"]])
               })
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!