Smooths every numeric column of a data frame by replacing each
value with the mean of a symmetric window of 2w + 1 observations
(the w rows before, the row itself, and the w rows after).
At the edges of the series the window is clipped to the available rows,
so the average is computed over fewer observations.
Value
A data frame with the same dimensions and column names as df
where each value has been replaced by its centred moving average.
Examples
compute_moving_average(df = mtcars, w = 5)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 20.50000 6.000000 211.8333 117.1667 3.440000 2.988333 18.12833 0.5000000 0.50000000 3.500000 2.166667
#> Mazda RX4 Wag 19.61429 6.285714 233.0000 135.4286 3.407143 3.071429 17.80143 0.4285714 0.42857143 3.428571 2.428571
#> Datsun 710 20.21250 6.000000 222.2125 126.2500 3.442500 3.086250 18.07625 0.5000000 0.37500000 3.500000 2.375000
#> Hornet 4 Drive 20.50000 5.777778 213.1667 122.7778 3.495556 3.093333 18.61222 0.5555556 0.33333333 3.555556 2.333333
#> Hornet Sportabout 20.37000 5.800000 208.6100 122.8000 3.538000 3.128000 18.58100 0.6000000 0.30000000 3.600000 2.500000
#> Valiant 20.13636 5.818182 204.8818 122.8182 3.572727 3.156364 18.61000 0.6363636 0.27272727 3.636364 2.636364
#> Duster 360 19.71818 6.000000 215.4091 129.1818 3.497273 3.288182 18.69545 0.6363636 0.18181818 3.545455 2.545455
#> Merc 240D 19.38182 6.181818 225.9364 135.5455 3.421818 3.365909 18.74818 0.6363636 0.09090909 3.454545 2.454545
#> Merc 230 18.69091 6.545455 241.1909 143.4545 3.350909 3.498636 18.69273 0.5454545 0.00000000 3.363636 2.636364
#> Merc 280 17.69091 6.727273 260.6455 152.0909 3.337273 3.683636 18.56000 0.4545455 0.00000000 3.363636 2.909091
#> Merc 280C 16.93636 6.727273 269.7364 155.7273 3.323636 3.864000 18.63273 0.4545455 0.00000000 3.363636 3.090909
#> Merc 450SE 16.62727 6.909091 289.2818 167.0909 3.366364 4.035364 18.37818 0.3636364 0.00000000 3.363636 3.363636
#> Merc 450SL 18.27273 6.545455 263.7091 150.8182 3.445455 3.910818 18.70818 0.4545455 0.09090909 3.454545 3.090909
#> Merc 450SLC 18.81818 6.545455 257.2545 149.9091 3.558182 3.767636 18.57364 0.4545455 0.18181818 3.454545 3.090909
#> Cadillac Fleetwood 19.82727 6.545455 250.9182 147.1818 3.585455 3.648091 18.30091 0.4545455 0.27272727 3.454545 3.000000
#> Lincoln Continental 20.03636 6.363636 246.6000 144.8182 3.565455 3.559455 18.45636 0.4545455 0.27272727 3.363636 2.727273
#> Chrysler Imperial 19.82727 6.545455 260.2727 147.2727 3.460000 3.566727 18.27182 0.3636364 0.27272727 3.272727 2.545455
#> Fiat 128 19.71818 6.545455 262.8364 144.5455 3.467273 3.509000 18.26273 0.3636364 0.27272727 3.272727 2.454545
#> Honda Civic 19.35455 6.545455 269.5818 150.4545 3.527273 3.519000 18.06364 0.3636364 0.27272727 3.272727 2.545455
#> Toyota Corolla 19.71818 6.545455 280.8727 150.0000 3.528182 3.524909 17.97727 0.3636364 0.27272727 3.272727 2.454545
#> Toyota Corona 21.25455 6.181818 245.1455 137.3636 3.632727 3.223545 18.06091 0.4545455 0.36363636 3.363636 2.181818
#> Dodge Challenger 22.67273 5.818182 214.2636 126.0909 3.762727 2.925000 17.95909 0.4545455 0.45454545 3.545455 2.000000
#> AMC Javelin 24.10000 5.454545 182.9091 115.4545 3.811818 2.576636 17.91182 0.5454545 0.54545455 3.727273 1.818182
#> Camaro Z28 22.59091 5.818182 207.6636 133.4545 3.824545 2.664818 17.46000 0.4545455 0.54545455 3.818182 2.090909
#> Pontiac Firebird 21.61818 6.000000 213.9636 144.6364 3.705455 2.769818 17.18545 0.3636364 0.54545455 3.909091 2.454545
#> Fiat X1-9 19.90000 6.363636 234.8636 169.1818 3.643636 2.927545 16.70364 0.2727273 0.54545455 4.000000 3.090909
#> Porsche 914-2 19.89091 6.363636 234.9455 170.2727 3.680909 2.956182 16.57545 0.2727273 0.63636364 4.090909 3.181818
#> Lotus Europa 20.33000 6.200000 226.6400 172.3000 3.773000 2.899800 16.54600 0.3000000 0.70000000 4.200000 3.300000
#> Ford Pantera L 20.90000 6.000000 218.0444 174.7778 3.842222 2.840333 16.46222 0.3333333 0.77777778 4.333333 3.444444
#> Ferrari Dino 21.85000 5.750000 201.5500 166.0000 3.856250 2.715375 16.59375 0.3750000 0.87500000 4.500000 3.375000
#> Maserati Bora 22.22857 5.428571 173.2000 164.7143 3.967143 2.554000 16.52857 0.4285714 1.00000000 4.714286 3.571429
#> Volvo 142E 21.38333 5.666667 188.9000 181.1667 3.948333 2.657167 16.13333 0.3333333 1.00000000 4.833333 4.000000