R在数据框架上滚动方程 [英] R rolling equation on data.frame

查看:143
本文介绍了R在数据框架上滚动方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是比较新的R,而且正在努力(A)构建滚动函数,(B)将该函数应用于整个数据框架。



以下等式是我正在为我的data.frame计算的:

  DSCOREx = {[(BAIX- 3 + BAIX-2 + BAIX-1)/ 3]  -  [(BAIX + BAIX + 1 + BAIX + 2)/ 3]} 
/ {√STDX*√(2/3)}

其中BAI Xn= Xn年的每个单元格中的值。

示例: BAI X-1= X年前一年的BAI值。



以下是一个示例数据框架:

 年份<  -  c(2000:2015)
S1 <-C(10,14,12,9,6,14,7,12, 11,8,9,15,9,10,7,11)
S2 <-C(5,8,9,14,12,10,8,7,11,9,14,8, 11,10,6,9)
S3< - c(12,13,9,8,11,7,10,9,11,14,8,6,10,9,12,14)
df< - rbind(Year,S1,S2,S3)
colnames(df)< - df [1,]
df < - df [-1,]

基本上我想应用equat对于每个样本(S1-S3),每年的每年价值(即BAI)为每个样本,并且每个样本的输出将是每年的DSCORE的新数据帧。



我从来没有写过一个函数或应用到一个data.frame中,所以我刚刚尝试将方程式放在函数形式中:

$ (x(x,y)=(x-3 + x-2)+ x- 1(3))^ 2)+(((x-1) - (((x-2 + x-1) x(x + 1 + x + 2)/ 3))^ 2))+(((x) - ((x + x + 1 + x + 2)/ 3) (x + 1) - ((x + x + 1 + x + 2)/ 3))^ 2)+(((x + 2) - ((x + x + 1 + x + 2)/ 3) ^(2)))/ 2}
DSCOREx < - function(x){(((x-3 + x-2 + x-1)/ 3) - ((x + x + 1 + 2)/ 3))/((sqrt(STDx))*(sqrt(2/3)))}

现在我不知道下一步应用这些函数到我的data.frame(或者如果它们正确写了!)。任何关于如何将函数应用于我的data.frame的建议将不胜感激。谢谢。

解决方案

您的问题对我来说并不完全清楚。你应该定义什么是X,n和STDX。但也许这正是你正在寻找的。

  DSCOREx<  -  matrix(0,nrow = nrow(df),ncol = ncol(df))
colnames(DSCOREx)< - colnames(df)
rownames(DSCOREx)< - rownames(df)

for(i in seq(1,nrow(df))){
for(j in seq(4,(ncol(df)-3))){
DSCOREx [i,j]< - (df [i,j-3] + df [i,j -2] + df [i,j-1] -df [i,j + 1] -df [i,j + 2] -df [i,j + 3])/ 3
}
}

我没有包含分母。


I'm still relatively new to R, and am struggling to (A) build a rolling function, and (B) apply that function to an entire data frame.

The following equation is what I'm trying to calculate for my data.frame:

DSCOREx  =  {[(BAIX-3 + BAIX-2 + BAIX-1)/3] - [(BAIX + BAIX+1 + BAIX+2)/3]}        
/ {√STDX * √(2/3)}

Where "BAI X-n" = the value in every cell for X-n year.
Example: "BAI X-1" = the BAI value for the year prior to X year.

Here is a sample data.frame:

Year <- c(2000:2015)    
S1 <- c(10,14,12,9,6,14,7,12,11,8,9,15,9,10,7,11)    
S2 <- c(5,8,9,14,12,10,8,7,11,9,14,8,11,10,6,9)    
S3 <- c(12,13,9,8,11,7,10,9,11,14,8,6,10,9,12,14)    
df <- rbind(Year, S1, S2, S3)    
colnames(df) <- df[1,]    
df <- df[-1,]    

Basically I'd like to apply the equation to every annual value (which is BAI) for each sample (S1-S3), and the output would be a new dataframe with a DSCORE for each year, for each sample.

I've never written a function or applied it to a data.frame, so I started by just trying to put the equation in function form:

STDx <- function (x) {(((((x-3)-((x-3 + x-2 + x-1)/3))^2)+(((x-2)-((x-3 + x-2 + x-1)/3))^2)+(((x-1)-((x-3 + x-2 + x-1)/3))^2))+((((x)-((x + x+1 + x+2)/3))^2)+(((x+1)-((x + x+1 + x+2)/3))^2)+(((x+2)-((x + x+1 + x+2)/3))^2)))/2}    
DSCOREx <- function(x) {(((x-3 + x-2 + x-1)/3) - ((x + x+1 + x+2)/3)) / ((sqrt(STDx)) * (sqrt(2/3)))}

Now I have no idea what to do next to apply the functions to my data.frame (or if they're written correctly!). Any suggestions on how to apply the function to my data.frame would be appreciated. Thanks.

解决方案

Your question is not entirely clear to me. You should define what X, n and STDX are. But perhaps this is what you're looking for.

DSCOREx <- matrix(0,nrow=nrow(df), ncol = ncol(df))
colnames(DSCOREx) <- colnames(df)
rownames(DSCOREx) <- rownames(df)

for (i in seq(1,nrow(df))){
   for (j in seq(4,(ncol(df)-3))){
      DSCOREx[i,j] <- (df[i,j-3] + df[i,j-2] + df[i,j-1] - df[i,j+1] - df[i,j+2] - df[i,j+3])/3
   }
}

I have not included the denominator.

这篇关于R在数据框架上滚动方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆