这是我的数据集的一个示例.我想基于每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)的时间序列平均值
发布评论