R中的滑动窗口

cha*_*has 5 r sliding-window dataframe

我有一个数据框 DF,下面显示了两列 A 和 B:

A                    B                  
1                    0             
3                    0               
4                    0                   
2                    1                    
6                    0                    
4                    1                     
7                    1                 
8                    1                     
1                    0   
Run Code Online (Sandbox Code Playgroud)

执行滑动窗口方法,如下所示。在大小为 3 的滑动窗口中计算列 B 的平均值,使用:rollapply(DF$B, width=3,by=1) 滑动 1。每个窗口的平均值显示在左侧。

    A:         1    3    4    2    6    4    7    8    1                                          
    B:         0    0    0    1    0    1    1    1    0                                
              [0    0    0]                                              0
                    [0    0    1]                                        0.33
                          [0    1    0]                                  0.33
                                [1    0    1]                            0.66
                                      [0    1    1]                      0.66
                                            [1    1    1]                1
                                                 [1    1    0]           0.66
output:        0   0.33 0.33 0.66   0.66    1     1    1   0.66
Run Code Online (Sandbox Code Playgroud)

现在,对于 A 列中的每一行/坐标,将考虑包含坐标的所有窗口,并应保留最高平均值,从而提供如“输出”列中所示的结果。

我需要获得如上所示的输出。输出应该是:

A                   B                  Output   
1                   0                      0
3                   0                      0.33
4                   0                      0.33
2                   1                      0.66
6                   0                      0.66
4                   1                      1
7                   1                      1
8                   1                      1
1                   0                    0.66
Run Code Online (Sandbox Code Playgroud)

在 R 中有任何帮助吗?

G. *_*eck 6

尝试这个:

# form input data
library(zoo)
B <- c(0, 0, 0, 1, 0, 1, 1, 1, 0)

# calculate
k <- 3
rollapply(B, 2*k-1, function(x) max(rollmean(x, k)), partial = TRUE)
Run Code Online (Sandbox Code Playgroud)

最后一行返回:

[1] 0.0000000 0.3333333 0.3333333 0.6666667 0.6666667 1.0000000 1.0000000
[8] 1.0000000 0.6666667
Run Code Online (Sandbox Code Playgroud)

如果有NA你可能想试试这个:

k <- 3
B <- c(1, 0, 1, 0, NA, 1)
rollapply(B, 2*k-1, function(x) max(rollapply(x, k, mean, na.rm = TRUE)), partial = TRUE)
Run Code Online (Sandbox Code Playgroud)

最后一行给出了这个:

[1] 0.6666667 0.6666667 0.6666667 0.5000000 0.5000000 0.5000000
Run Code Online (Sandbox Code Playgroud)

将其扩展为:

c(mean(B[1:3], na.rm = TRUE), ##
max(mean(B[1:3], na.rm = TRUE), mean(B[2:4], na.rm = TRUE)), ##
max(mean(B[1:3], na.rm = TRUE), mean(B[2:4], na.rm = TRUE), mean(B[3:5], na.rm = TRUE)),
max(mean(B[2:4], na.rm = TRUE), mean(B[3:5], na.rm = TRUE), mean(B[4:6], na.rm = TRUE)),
max(mean(B[3:5], na.rm = TRUE), mean(B[4:6], na.rm = TRUE)), ##
mean(B[4:6], na.rm = TRUE)) ##
Run Code Online (Sandbox Code Playgroud)

如果您不希望k-1每端的组件(用##上面标记) drop partial = TRUE