7 mapreduce()

Base R has functionals that output a list by way of lapply() and Map() (among others), while Reduce() diminishes given elements in a consecutive manner until a single result remains. While these functions are relatively straightforward to combine (depending on the functions being passed), R does not inherently possess a singular function to accomplish this operation unlike Julia with mapreduce()8. Therefore, mapreduce() in afp offers a simplified Julia-equivalent to streamline the intended procedure in R.

The three required parameters are f, o, and x (“fox,” collectively)–function, (binary) operator, and collection (e.g. matrix). If f is multivariate, the fourth parameter y can take multiple arguments much like MoreArgs in mapply(). The final parameter ... passes to Reduce(). The output is typically a matrix, depending on the inputs and functions being passed.

7.0.1 Map and Reduce

# GOAL: Map a function to each matrix and then reduce them by a binary operator.

matrixl <- list(A = matrix(c(1:9), 3, 3), 
                B = matrix(10:18, 3, 3), 
                C = matrix(19:27, 3, 3))

## Univariate case
output1 <- mapreduce(function(x) x^2 + 1, `/`, matrixl) 

output1 ## == Reduce(`/`, Map(function(x) x^2 + 1, matrixl))
##              [,1]         [,2]         [,3]
## [1,] 0.0000547016 0.0002061856 0.0003107868
## [2,] 0.0001022035 0.0002490183 0.0003310752
## [3,] 0.0001560306 0.0002837380 0.0003456270
## Multivariate case
output2 <- with(matrixl, 
                mapreduce(function(i, j, k) i*j - k, 
                          `/`, 
                          A, 
                          list(B, C)))

output2
##               [,1]          [,2]          [,3]
## [1,] -1.387799e-10 -3.408547e-12 -3.442132e-13
## [2,] -2.925642e-11 -1.456427e-12 -1.839155e-13
## [3,] -9.059628e-12 -6.829299e-13 -1.031438e-13

7.1 mrchop()

The function mrchop() has similar properties to mapreduce(): it applies the latter row-wise or column-wise, which can be specified.

The four required parameters are f, o, x, and m–function, (binary) operator, collection (e.g. matrix), and margin (either 1 for row-wise or 2 for column-wise). The fifth, optional parameter ... passes to mapreduce(), which passes to Reduce(). The output is typically a matrix, depending on the inputs and functions being passed.

7.1.1 Map and Reduce Column/Row-wise

mrchop(function(x) x/2, `+`, mtcars, 1) # Row-wise
##           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
##            164.4900            164.8975            129.7900            213.0675 
##   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
##            295.1550            192.7700            328.4600            135.4900 
##            Merc 230            Merc 280           Merc 280C          Merc 450SE 
##            149.7850            175.2300            174.8300            255.3700 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
##            255.7500            254.9250            364.2800            363.3220 
##   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
##            362.8475            106.9250             97.5825            103.4775 
##       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
##            136.8875            259.8250            253.0425            323.1400 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
##            315.5875            104.1075            136.2850            136.8415 
##      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
##            335.3450            189.7950            347.3550            144.4450
mrchop(function(x) x/2, `+`, mtcars, 2) # Column-wise
##      mpg      cyl     disp       hp     drat       wt     qsec       vs 
##  321.450   99.000 3691.550 2347.000   57.545   51.476  285.580    7.000 
##       am     gear     carb 
##    6.500   59.000   45.000

7.2 reducechop()

The function reducechop() has similar properties to mrchop(): it applies Reduce() row-wise or column-wise, which can be specified.

7.2.1 Reduce Column/Row-wise

reducechop(`+`, mtcars, 1) # Row-wise. Equivalent to Reduce(`+`, mtcars). 
##           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
##             328.980             329.795             259.580             426.135 
##   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
##             590.310             385.540             656.920             270.980 
##            Merc 230            Merc 280           Merc 280C          Merc 450SE 
##             299.570             350.460             349.660             510.740 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
##             511.500             509.850             728.560             726.644 
##   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
##             725.695             213.850             195.165             206.955 
##       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
##             273.775             519.650             506.085             646.280 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
##             631.175             208.215             272.570             273.683 
##      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
##             670.690             379.590             694.710             288.890
reducechop(`+`, mtcars, 2) # Column-wise (default).
##      mpg      cyl     disp       hp     drat       wt     qsec       vs 
##  642.900  198.000 7383.100 4694.000  115.090  102.952  571.160   14.000 
##       am     gear     carb 
##   13.000  118.000   90.000