在R中滑动窗口 [英] Sliding window in R
问题描述
我有一个数据框架DF,两列A和B如下所示:
AB
1 0
3 0
4 0
2 1
6 0
4 1
7 1
8 1
1 0
如下所示执行滑动窗口方法。平均值在大小为3的滑动窗口中的列B被计算为:使用rollapply(DF $ B,width = 3,= 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
输出:0 0.33 0.33 0.66 0.66 1 1 1 0.66
现在,对于列A中的每行/坐标,考虑包含坐标的所有窗口,并应保留最高平均值,给出结果列输出。
我需要获得如上所示的输出。输出应该如下:
AB输出
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
在R中有任何帮助
尝试这样:
#表单输入数据
库(zoo)
B < c(0,0,0,1,0,1,1,1,0)
#计算
k< - 3
rollapply(B,2 * 1,function(x)max(rollmean(x,k)),partial = TRUE)
最后一行返回:
[1] 0.0000000 0.3333333 0 .3333333 0.6666667 0.6666667 1.0000000 1.0000000
[8] 1.0000000 0.6666667
如果有 NA
值,您可能需要尝试以下方式:
(b,2 * k-1,函数(x)max(rollapply(x)) ,k,mean,na.rm = TRUE)),partial = TRUE)
最后一行给出这一点:
[1] 0.6666667 0.6666667 0.6666667 0.5000000 0.5000000 0.5000000
扩展它们形成为:
c(mean (B [1:3],na.rm = TRUE),##
max(mean(B [1:3],na.rm = TRUE),mean(B [2:4],na。 (B [2:4],na.rm = TRUE),mean(B [1:3],na.rm = TRUE),mean 3:5],na.rm = TRUE)),
max(mean(B [2:4],na.rm = TRUE),mean(B [3:5],na.rm = ,mean(B [4:6],na.rm = TRUE)),
max(mean(B [3:5],na.rm = TRUE),mean(B [4:6] .rm = TRUE)),##
表示(B [4:6],na.rm = TRUE))##
如果您不想要 k-1
在每个结尾的组件(标有 ##
以上)drop partial = TRUE
。 / p>
I have a dataframe DF, with two columns A and B shown below:
A B
1 0
3 0
4 0
2 1
6 0
4 1
7 1
8 1
1 0
A sliding window approach is performed as shown below. The mean is calulated for column B in a sliding window of size 3 sliding by 1 using: rollapply(DF$B, width=3,by=1). The mean values for each window are shown on the left side.
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
Now, for each row/coordinate in column A, all windows containing the coordinate are considered and should retain the highest mean value which gives the results as shown in column 'output'.
I need to obtain the output as shown above. The output should like:
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
Any help in R?
Try this:
# 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)
The last line returns:
[1] 0.0000000 0.3333333 0.3333333 0.6666667 0.6666667 1.0000000 1.0000000
[8] 1.0000000 0.6666667
If there are NA
values you might want to try this:
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)
where the last line gives this:
[1] 0.6666667 0.6666667 0.6666667 0.5000000 0.5000000 0.5000000
Expanding it out these are formed as:
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)) ##
If you don't want the k-1
components at each end (marked with ##
above) drop partial = TRUE
.
这篇关于在R中滑动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!