Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Correct way to convolve more than two variables in R

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A Python post with a similar question
An R post about auto-convolution

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading