Is there a correct way to convolute (convolve()
) more than two variables in R
? This is a toy dataset:
df = data.frame(
A = c(-0.315, -0.055, -0.017, -1.181, -0.082),
B = c(-0.159, -0.455, 0.494, -0.672, -0.691),
C = c(0.408, -0.161, 0.308, 0.305, -0.122),
D = c(0.371, -0.511, 0.025, -0.107, 0.804)
)
I was thinking of convolving the first two, take that result and convolving it with the third, and that result with the fourth and so on. For example:
c = convolve(df[,1], df[,2])
c = convolve(c, df[,3])
c = convolve(c, df[,4])
...
If this is the correct way, what would be an efficient way to implement this, assuming that the number of columns (variables) can change?
A Python post with a similar question
An R post about auto-convolution
Use Reduce
Reduce(convolve, df)
## [1] 0.54181086 -0.04707215 0.26347838 0.54920754 -0.25642045
# check
c <- convolve(df[,1], df[,2])
c <- convolve(c, df[,3])
c <- convolve(c, df[,4])
c
## [1] 0.54181086 -0.04707215 0.26347838 0.54920754 -0.25642045