input = cbind(c(3,7,3,5,2,9,1,4,6,4,7,3,7,4))
library(zoo)
output = cbind(rollmean(input,4))
print(input)
print(output)
输出:
[,1]
[1,] 3
[2,] 7
[3,] 3
[4,] 5
[5,] 2
[6,] 9
[7,] 1
[8,] 4
[9,] 6
[10,] 4
[11,] 7
[12,] 3
[13,] 7
[14,] 4
[,1]
[1,] 4.50
[2,] 4.25
[3,] 4.75
[4,] 4.25
[5,] 4.00
[6,] 5.00
[7,] 3.75
[8,] 5.25
[9,] 5.00
[10,] 5.25
[11,] 5.25
但是当我尝试绑定它时:
Error in cbind(input, output) :
number of rows of matrices must match (see arg 2)
Calls: print -> cbind
Execution halted
我想使用一个足够聪明的函数,如果它没有在向量的两端获取数据并仅根据它拥有的数据计算输出,则不要放弃。因此,例如在 input[1] 中,它将仅计算右侧的平均值