基于定义的时间间隔(bin)的时间序列平均值

编程入门 行业动态 更新时间:2024-10-24 16:29:48
本文介绍了基于定义的时间间隔(bin)的时间序列平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的数据集的一个示例.我想基于每10秒的时间(即ts)来计算bin平均.您能提供一些提示,以便我继续吗?

Here is an example of my dataset. I want to calculate bin average based on time (i.e., ts) every 10 seconds. Could you please provide some hints so that I can carry on?

就我而言,我想平均每10秒的时间(ts)和无功.例如,我将获得从0到10秒的Var和ts的平均值;我将在11到20秒之间得到另一个Var和ts的平均值,等等.

In my case, I want to average time (ts) and Var in every 10 seconds. For example, I will get an averaged value of Var and ts from 0 to 10 seconds; I will get another averaged value of Var and ts from 11 to 20 seconds, etc.

df = data.frame(ts = seq(1,100,by=0.5), Var = runif(199,1, 10))

R中的任何函数或库都可以用于此任务吗?

Any functions or libraries in R can I use for this task?

推荐答案

有很多方法可以计算装箱平均值:使用底数aggregate,by,使用软件包dplyr,data.table,可能使用zoo,当然还有其他时间序列包...

There are many ways to calculate a binned average: with base aggregate,by, with the packages dplyr, data.table, probably with zoo and surely other timeseries packages...

library(dplyr) df %>% group_by(interval = round(df$ts/10)*10) %>% summarize(Var_mean = mean(Var)) # A tibble: 11 x 2 interval Var_mean <dbl> <dbl> 1 0 4.561653 2 10 6.544980 3 20 6.110336 4 30 4.288523 5 40 5.339249 6 50 6.811147 7 60 6.180795 8 70 4.920476 9 80 5.486937 10 90 5.284871 11 100 5.917074

这是dplyr的方法,请查看它和data.table如何让您命名中间变量,从而使代码清晰易读.

That's the dplyr approach, see how it and data.table let you name the intermediate variables, which keeps code clean and legible.

更多推荐

基于定义的时间间隔(bin)的时间序列平均值

本文发布于:2023-10-23 22:59:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   平均值   间隔   序列   定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!