6 mapdims()

The function mapdims() calls apply() to map over a dataset’s dimensions, saving the column- and row-wise results separately in a list.

The required inputs for these functions are f and x, respectively the function to execute and the dataset over which to perform the function. The output is a list of arrays (typically a vector or matrix, depending on the function being passed).

6.0.1 Mapping dimensions

mapdims(median, mtcars)
## $rowwise
##           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
##               4.000               4.000               4.000               3.215 
##   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
##               3.440               3.460               4.000               4.000 
##            Merc 230            Merc 280           Merc 280C          Merc 450SE 
##               4.000               4.000               4.000               4.070 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
##               3.730               3.780               5.250               5.424 
##   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
##               5.345               4.000               4.000               4.000 
##       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
##               3.700               3.520               3.435               4.000 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
##               3.845               4.000               4.430               4.000 
##      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
##               5.000               6.000               8.000               4.000 
## 
## $colwise
##     mpg     cyl    disp      hp    drat      wt    qsec      vs      am    gear 
##  19.200   6.000 196.300 123.000   3.695   3.325  17.710   0.000   0.000   4.000 
##    carb 
##   2.000

6.1 mapc() and mapr()

To apply a function column-wise in R, apply(x, 2, f) can be called–for row-wise results, the margin input (i.e., the second input) can be set to 1. For situational convenience, the functions mapc() and mapr() achieve the same results, respectively.

The required inputs for these functions are f and x, respectively the function to execute and the dataset over which to perform the function. The output is an array (typically a vector or matrix, depending on the function being passed).

6.1.1 mapc/r()

mapc(median, mtcars) # Column-wise results
##     mpg     cyl    disp      hp    drat      wt    qsec      vs      am    gear 
##  19.200   6.000 196.300 123.000   3.695   3.325  17.710   0.000   0.000   4.000 
##    carb 
##   2.000
mapr(median, mtcars) # Row-wise results.
##           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
##               4.000               4.000               4.000               3.215 
##   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
##               3.440               3.460               4.000               4.000 
##            Merc 230            Merc 280           Merc 280C          Merc 450SE 
##               4.000               4.000               4.000               4.070 
##          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
##               3.730               3.780               5.250               5.424 
##   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
##               5.345               4.000               4.000               4.000 
##       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
##               3.700               3.520               3.435               4.000 
##    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
##               3.845               4.000               4.430               4.000 
##      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
##               5.000               6.000               8.000               4.000