[英]Rolling root mean square instead of mean in r
Let's say I have 1-hour data and I can aggregate the data to 2-hour and 3-hour data by rolling mean with the codes below.假设我有 1 小时的数据,我可以使用以下代码通过滚动平均值将数据聚合为 2 小时和 3 小时的数据。
library(zoo)
h1<-c(2, 4, 6)
h2<-rollmean(h1,2)
h3<-rollmean(h1,3)
Here's what I got from this:这是我从中得到的:
h2
[1] 3 5
h3
[1] 4
But I want to use root mean square
function below instead of mean
function while aggregating.但是我想在聚合时使用下面的root mean square
function 而不是mean
function。
sqrt(mean(x^2))
Here are the desired outputs:以下是所需的输出:
h2_rms
[1] 3.162278 5.099020
h3_rms
[1] 4.320494
You can apply your own functions with rollapply
:您可以使用rollapply
应用您自己的函数:
library(zoo)
h1<-c(2, 4, 6)
rms <- function(x) sqrt(mean(x^2))
h2 <- rollapply(h1,2, rms)
h3 <- rollapply(h1,3, rms)
h2
#[1] 3.162278 5.099020
h3
#[1] 4.320494
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.