Fast operators to perform row- or column-wise replacing and sweeping operations of vectors on matrices, data frames, lists.

## Perform the operation with v and each row of X

X %rr% v    # Replace rows of X with v
X %r+% v    # Add v to each row of X
X %r-% v    # Subtract v from each row of X
X %r*% v    # Multiply each row of X with v
X %r/% v    # Divide each row of X by v

## Perform the operation with v and each column of X

X %cr% v    # Replace columns of X with v
X %c+% v    # Add v to each column of X
X %c-% v    # Subtract v from each column of X
X %c*% v    # Multiply each column of X with v
X %c/% v    # Divide each column of X by v

## Arguments

X a vector, matrix, data frame or list like object (with rows (r) columns (c) matching v). a suitable atomic vector. If X is a list, v can also be a scalar or list of vectors (for column-operations) or a list of scalar atomic elements (for row-operations on data frame's). It is also possible to sweep lists of vectors v out of lists of matrices X (or even lists of data frames X for row-operations).

## Details

With a matrix or data frame X, the default behavior of R when calling X op v (such as multiplication X * v) is to perform the operation of v with each column of X. The equivalent operation is performed by X %cop% v, with the difference that it computes significantly faster if X is a data frame / list. A more complex but frequently required task is to perform an operation with v on each row of X. This is provided based on efficient C++ code by the %rop% set of functions, e.g. X %r*% v efficiently multiplies v to each row of X.

## Value

X where the operation with v was performed on each row or column. All attributes of X are preserved.

## Note

Computations and Output: These functions are all quite simple, they only work with X on the LHS i.e. v %op% X will likely fail. The row operations are simple wrappers around TRA which provides more operations including grouped replacing and sweeping (where v would be a matrix or data frame with less rows than X being mapped to the rows of X by grouping vectors). One consequence is that just like TRA, row-wise mathematical operations (+, -, *, /) always yield numeric output, even if both X and v may be integer. This is different for column- operations which depend on base R and may also preserve integer data.

Rules of Arithmetic: Since these operators are defined as simple infix functions, the normal rules of arithmetic are not respected. So a %c+% b %c*% c evaluates as (a %c+% b) %c*% c. As with all chained infix operations, they are just evaluated sequentially from left to right.

Performance Notes: For Fast Statistical Functions, using fmedian(X, TRA = "-") will be a tiny bit faster than X %r-% fmedian(X). Also use fwithin(X) for fast centering using the mean, and fscale(X) for fast scaling and centering or mean-preserving scaling.

TRA, dapply, Data Transformations, Collapse Overview

## Examples

## Using data frame's / lists
v <- mtcars\$cyl
mtcars %cr% v
#>                     mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4             6   6    6  6    6  6    6  6  6    6    6
#> Mazda RX4 Wag         6   6    6  6    6  6    6  6  6    6    6
#> Datsun 710            4   4    4  4    4  4    4  4  4    4    4
#> Hornet 4 Drive        6   6    6  6    6  6    6  6  6    6    6
#> Hornet Sportabout     8   8    8  8    8  8    8  8  8    8    8
#> Valiant               6   6    6  6    6  6    6  6  6    6    6
#> Duster 360            8   8    8  8    8  8    8  8  8    8    8
#> Merc 240D             4   4    4  4    4  4    4  4  4    4    4
#> Merc 230              4   4    4  4    4  4    4  4  4    4    4
#> Merc 280              6   6    6  6    6  6    6  6  6    6    6
#> Merc 280C             6   6    6  6    6  6    6  6  6    6    6
#> Merc 450SE            8   8    8  8    8  8    8  8  8    8    8
#> Merc 450SL            8   8    8  8    8  8    8  8  8    8    8
#> Merc 450SLC           8   8    8  8    8  8    8  8  8    8    8
#> Cadillac Fleetwood    8   8    8  8    8  8    8  8  8    8    8
#> Lincoln Continental   8   8    8  8    8  8    8  8  8    8    8
#> Chrysler Imperial     8   8    8  8    8  8    8  8  8    8    8
#> Fiat 128              4   4    4  4    4  4    4  4  4    4    4
#> Honda Civic           4   4    4  4    4  4    4  4  4    4    4
#> Toyota Corolla        4   4    4  4    4  4    4  4  4    4    4
#> Toyota Corona         4   4    4  4    4  4    4  4  4    4    4
#> Dodge Challenger      8   8    8  8    8  8    8  8  8    8    8
#> AMC Javelin           8   8    8  8    8  8    8  8  8    8    8
#> Camaro Z28            8   8    8  8    8  8    8  8  8    8    8
#> Pontiac Firebird      8   8    8  8    8  8    8  8  8    8    8
#> Fiat X1-9             4   4    4  4    4  4    4  4  4    4    4
#> Porsche 914-2         4   4    4  4    4  4    4  4  4    4    4
#> Lotus Europa          4   4    4  4    4  4    4  4  4    4    4
#> Ford Pantera L        8   8    8  8    8  8    8  8  8    8    8
#> Ferrari Dino          6   6    6  6    6  6    6  6  6    6    6
#> Maserati Bora         8   8    8  8    8  8    8  8  8    8    8
#> Volvo 142E            4   4    4  4    4  4    4  4  4    4    4mtcars %c-% v
#>                      mpg cyl  disp  hp  drat     wt  qsec vs am gear carb
#> Mazda RX4           15.0   0 154.0 104 -2.10 -3.380 10.46 -6 -5   -2   -2
#> Mazda RX4 Wag       15.0   0 154.0 104 -2.10 -3.125 11.02 -6 -5   -2   -2
#> Datsun 710          18.8   0 104.0  89 -0.15 -1.680 14.61 -3 -3    0   -3
#> Hornet 4 Drive      15.4   0 252.0 104 -2.92 -2.785 13.44 -5 -6   -3   -5
#> Hornet Sportabout   10.7   0 352.0 167 -4.85 -4.560  9.02 -8 -8   -5   -6
#> Valiant             12.1   0 219.0  99 -3.24 -2.540 14.22 -5 -6   -3   -5
#> Duster 360           6.3   0 352.0 237 -4.79 -4.430  7.84 -8 -8   -5   -4
#> Merc 240D           20.4   0 142.7  58 -0.31 -0.810 16.00 -3 -4    0   -2
#> Merc 230            18.8   0 136.8  91 -0.08 -0.850 18.90 -3 -4    0   -2
#> Merc 280            13.2   0 161.6 117 -2.08 -2.560 12.30 -5 -6   -2   -2
#> Merc 280C           11.8   0 161.6 117 -2.08 -2.560 12.90 -5 -6   -2   -2
#> Merc 450SE           8.4   0 267.8 172 -4.93 -3.930  9.40 -8 -8   -5   -5
#> Merc 450SL           9.3   0 267.8 172 -4.93 -4.270  9.60 -8 -8   -5   -5
#> Merc 450SLC          7.2   0 267.8 172 -4.93 -4.220 10.00 -8 -8   -5   -5
#> Cadillac Fleetwood   2.4   0 464.0 197 -5.07 -2.750  9.98 -8 -8   -5   -4
#> Lincoln Continental  2.4   0 452.0 207 -5.00 -2.576  9.82 -8 -8   -5   -4
#> Chrysler Imperial    6.7   0 432.0 222 -4.77 -2.655  9.42 -8 -8   -5   -4
#> Fiat 128            28.4   0  74.7  62  0.08 -1.800 15.47 -3 -3    0   -3
#> Honda Civic         26.4   0  71.7  48  0.93 -2.385 14.52 -3 -3    0   -2
#> Toyota Corolla      29.9   0  67.1  61  0.22 -2.165 15.90 -3 -3    0   -3
#> Toyota Corona       17.5   0 116.1  93 -0.30 -1.535 16.01 -3 -4   -1   -3
#> Dodge Challenger     7.5   0 310.0 142 -5.24 -4.480  8.87 -8 -8   -5   -6
#> AMC Javelin          7.2   0 296.0 142 -4.85 -4.565  9.30 -8 -8   -5   -6
#> Camaro Z28           5.3   0 342.0 237 -4.27 -4.160  7.41 -8 -8   -5   -4
#> Pontiac Firebird    11.2   0 392.0 167 -4.92 -4.155  9.05 -8 -8   -5   -6
#> Fiat X1-9           23.3   0  75.0  62  0.08 -2.065 14.90 -3 -3    0   -3
#> Porsche 914-2       22.0   0 116.3  87  0.43 -1.860 12.70 -4 -3    1   -2
#> Lotus Europa        26.4   0  91.1 109 -0.23 -2.487 12.90 -3 -3    1   -2
#> Ford Pantera L       7.8   0 343.0 256 -3.78 -4.830  6.50 -8 -7   -3   -4
#> Ferrari Dino        13.7   0 139.0 169 -2.38 -3.230  9.50 -6 -5   -1    0
#> Maserati Bora        7.0   0 293.0 327 -4.46 -4.430  6.60 -8 -7   -3    0
#> Volvo 142E          17.4   0 117.0 105  0.11 -1.220 14.60 -3 -3    0   -2mtcars %r-% seq_col(mtcars)
#>                      mpg cyl  disp  hp  drat     wt  qsec vs am gear carb
#> Mazda RX4           20.0   4 157.0 106 -1.10 -3.380  9.46 -8 -8   -6   -7
#> Mazda RX4 Wag       20.0   4 157.0 106 -1.10 -3.125 10.02 -8 -8   -6   -7
#> Datsun 710          21.8   2 105.0  89 -1.15 -3.680 11.61 -7 -8   -6  -10
#> Hornet 4 Drive      20.4   4 255.0 106 -1.92 -2.785 12.44 -7 -9   -7  -10
#> Hornet Sportabout   17.7   6 357.0 171 -1.85 -2.560 10.02 -8 -9   -7   -9
#> Valiant             17.1   4 222.0 101 -2.24 -2.540 13.22 -7 -9   -7  -10
#> Duster 360          13.3   6 357.0 241 -1.79 -2.430  8.84 -8 -9   -7   -7
#> Merc 240D           23.4   2 143.7  58 -1.31 -2.810 13.00 -7 -9   -6   -9
#> Merc 230            21.8   2 137.8  91 -1.08 -2.850 15.90 -7 -9   -6   -9
#> Merc 280            18.2   4 164.6 119 -1.08 -2.560 11.30 -7 -9   -6   -7
#> Merc 280C           16.8   4 164.6 119 -1.08 -2.560 11.90 -7 -9   -6   -7
#> Merc 450SE          15.4   6 272.8 176 -1.93 -1.930 10.40 -8 -9   -7   -8
#> Merc 450SL          16.3   6 272.8 176 -1.93 -2.270 10.60 -8 -9   -7   -8
#> Merc 450SLC         14.2   6 272.8 176 -1.93 -2.220 11.00 -8 -9   -7   -8
#> Cadillac Fleetwood   9.4   6 469.0 201 -2.07 -0.750 10.98 -8 -9   -7   -7
#> Lincoln Continental  9.4   6 457.0 211 -2.00 -0.576 10.82 -8 -9   -7   -7
#> Chrysler Imperial   13.7   6 437.0 226 -1.77 -0.655 10.42 -8 -9   -7   -7
#> Fiat 128            31.4   2  75.7  62 -0.92 -3.800 12.47 -7 -8   -6  -10
#> Honda Civic         29.4   2  72.7  48 -0.07 -4.385 11.52 -7 -8   -6   -9
#> Toyota Corolla      32.9   2  68.1  61 -0.78 -4.165 12.90 -7 -8   -6  -10
#> Toyota Corona       20.5   2 117.1  93 -1.30 -3.535 13.01 -7 -9   -7  -10
#> Dodge Challenger    14.5   6 315.0 146 -2.24 -2.480  9.87 -8 -9   -7   -9
#> AMC Javelin         14.2   6 301.0 146 -1.85 -2.565 10.30 -8 -9   -7   -9
#> Camaro Z28          12.3   6 347.0 241 -1.27 -2.160  8.41 -8 -9   -7   -7
#> Pontiac Firebird    18.2   6 397.0 171 -1.92 -2.155 10.05 -8 -9   -7   -9
#> Fiat X1-9           26.3   2  76.0  62 -0.92 -4.065 11.90 -7 -8   -6  -10
#> Porsche 914-2       25.0   2 117.3  87 -0.57 -3.860  9.70 -8 -8   -5   -9
#> Lotus Europa        29.4   2  92.1 109 -1.23 -4.487  9.90 -7 -8   -5   -9
#> Ford Pantera L      14.8   6 348.0 260 -0.78 -2.830  7.50 -8 -8   -5   -7
#> Ferrari Dino        18.7   4 142.0 171 -1.38 -3.230  8.50 -8 -8   -5   -5
#> Maserati Bora       14.0   6 298.0 331 -1.46 -2.430  7.60 -8 -8   -5   -3
#> Volvo 142E          20.4   2 118.0 105 -0.89 -3.220 11.60 -7 -8   -6   -9mtcars %r-% lapply(mtcars, quantile, 0.28)
#>                        mpg cyl    disp     hp    drat     wt    qsec vs am gear
#> Mazda RX4            5.296   2  25.536   7.56  0.7724 -0.102 -0.5216  0  1    1
#> Mazda RX4 Wag        5.296   2  25.536   7.56  0.7724  0.153  0.0384  0  1    1
#> Datsun 710           7.096   0 -26.464  -9.44  0.7224 -0.402  1.6284  1  1    1
#> Hornet 4 Drive       5.696   2 123.536   7.56 -0.0476  0.493  2.4584  1  0    0
#> Hornet Sportabout    2.996   4 225.536  72.56  0.0224  0.718  0.0384  0  0    0
#> Valiant              2.396   2  90.536   2.56 -0.3676  0.738  3.2384  1  0    0
#> Duster 360          -1.404   4 225.536 142.56  0.0824  0.848 -1.1416  0  0    0
#> Merc 240D            8.696   0  12.236 -40.44  0.5624  0.468  3.0184  1  0    1
#> Merc 230             7.096   0   6.336  -7.44  0.7924  0.428  5.9184  1  0    1
#> Merc 280             3.496   2  33.136  20.56  0.7924  0.718  1.3184  1  0    1
#> Merc 280C            2.096   2  33.136  20.56  0.7924  0.718  1.9184  1  0    1
#> Merc 450SE           0.696   4 141.336  77.56 -0.0576  1.348  0.4184  0  0    0
#> Merc 450SL           1.596   4 141.336  77.56 -0.0576  1.008  0.6184  0  0    0
#> Merc 450SLC         -0.504   4 141.336  77.56 -0.0576  1.058  1.0184  0  0    0
#> Cadillac Fleetwood  -5.304   4 337.536 102.56 -0.1976  2.528  0.9984  0  0    0
#> Lincoln Continental -5.304   4 325.536 112.56 -0.1276  2.702  0.8384  0  0    0
#> Chrysler Imperial   -1.004   4 305.536 127.56  0.1024  2.623  0.4384  0  0    0
#> Fiat 128            16.696   0 -55.764 -36.44  0.9524 -0.522  2.4884  1  1    1
#> Honda Civic         14.696   0 -58.764 -50.44  1.8024 -1.107  1.5384  1  1    1
#> Toyota Corolla      18.196   0 -63.364 -37.44  1.0924 -0.887  2.9184  1  1    1
#> Toyota Corona        5.796   0 -14.364  -5.44  0.5724 -0.257  3.0284  1  0    0
#> Dodge Challenger    -0.204   4 183.536  47.56 -0.3676  0.798 -0.1116  0  0    0
#> AMC Javelin         -0.504   4 169.536  47.56  0.0224  0.713  0.3184  0  0    0
#> Camaro Z28          -2.404   4 215.536 142.56  0.6024  1.118 -1.5716  0  0    0
#> Pontiac Firebird     3.496   4 265.536  72.56 -0.0476  1.123  0.0684  0  0    0
#> Fiat X1-9           11.596   0 -55.464 -36.44  0.9524 -0.787  1.9184  1  1    1
#> Porsche 914-2       10.296   0 -14.164 -11.44  1.3024 -0.582 -0.2816  0  1    2
#> Lotus Europa        14.696   0 -39.364  10.56  0.6424 -1.209 -0.0816  1  1    2
#> Ford Pantera L       0.096   4 216.536 161.56  1.0924  0.448 -2.4816  0  1    2
#> Ferrari Dino         3.996   2  10.536  72.56  0.4924  0.048 -1.4816  0  1    2
#> Maserati Bora       -0.704   4 166.536 232.56  0.4124  0.848 -2.3816  0  1    2
#> Volvo 142E           5.696   0 -13.464   6.56  0.9824  0.058  1.6184  1  1    1
#>                     carb
#> Mazda RX4              2
#> Mazda RX4 Wag          2
#> Datsun 710            -1
#> Hornet 4 Drive        -1
#> Valiant               -1
#> Duster 360             2
#> Merc 240D              0
#> Merc 230               0
#> Merc 280               2
#> Merc 280C              2
#> Merc 450SE             1
#> Merc 450SL             1
#> Merc 450SLC            1
#> Lincoln Continental    2
#> Chrysler Imperial      2
#> Fiat 128              -1
#> Honda Civic            0
#> Toyota Corolla        -1
#> Toyota Corona         -1
#> Dodge Challenger       0
#> AMC Javelin            0
#> Camaro Z28             2
#> Pontiac Firebird       0
#> Fiat X1-9             -1
#> Porsche 914-2          0
#> Lotus Europa           0
#> Ford Pantera L         2
#> Ferrari Dino           4
#> Maserati Bora          6
#> Volvo 142E             0
mtcars %c*% 5       # Significantly faster than mtcars * 5
#>                       mpg cyl   disp   hp  drat     wt   qsec vs am gear carb
#> Mazda RX4           105.0  30  800.0  550 19.50 13.100  82.30  0  5   20   20
#> Mazda RX4 Wag       105.0  30  800.0  550 19.50 14.375  85.10  0  5   20   20
#> Datsun 710          114.0  20  540.0  465 19.25 11.600  93.05  5  5   20    5
#> Hornet 4 Drive      107.0  30 1290.0  550 15.40 16.075  97.20  5  0   15    5
#> Hornet Sportabout    93.5  40 1800.0  875 15.75 17.200  85.10  0  0   15   10
#> Valiant              90.5  30 1125.0  525 13.80 17.300 101.10  5  0   15    5
#> Duster 360           71.5  40 1800.0 1225 16.05 17.850  79.20  0  0   15   20
#> Merc 240D           122.0  20  733.5  310 18.45 15.950 100.00  5  0   20   10
#> Merc 230            114.0  20  704.0  475 19.60 15.750 114.50  5  0   20   10
#> Merc 280             96.0  30  838.0  615 19.60 17.200  91.50  5  0   20   20
#> Merc 280C            89.0  30  838.0  615 19.60 17.200  94.50  5  0   20   20
#> Merc 450SE           82.0  40 1379.0  900 15.35 20.350  87.00  0  0   15   15
#> Merc 450SL           86.5  40 1379.0  900 15.35 18.650  88.00  0  0   15   15
#> Merc 450SLC          76.0  40 1379.0  900 15.35 18.900  90.00  0  0   15   15
#> Cadillac Fleetwood   52.0  40 2360.0 1025 14.65 26.250  89.90  0  0   15   20
#> Lincoln Continental  52.0  40 2300.0 1075 15.00 27.120  89.10  0  0   15   20
#> Chrysler Imperial    73.5  40 2200.0 1150 16.15 26.725  87.10  0  0   15   20
#> Fiat 128            162.0  20  393.5  330 20.40 11.000  97.35  5  5   20    5
#> Honda Civic         152.0  20  378.5  260 24.65  8.075  92.60  5  5   20   10
#> Toyota Corolla      169.5  20  355.5  325 21.10  9.175  99.50  5  5   20    5
#> Toyota Corona       107.5  20  600.5  485 18.50 12.325 100.05  5  0   15    5
#> Dodge Challenger     77.5  40 1590.0  750 13.80 17.600  84.35  0  0   15   10
#> AMC Javelin          76.0  40 1520.0  750 15.75 17.175  86.50  0  0   15   10
#> Camaro Z28           66.5  40 1750.0 1225 18.65 19.200  77.05  0  0   15   20
#> Pontiac Firebird     96.0  40 2000.0  875 15.40 19.225  85.25  0  0   15   10
#> Fiat X1-9           136.5  20  395.0  330 20.40  9.675  94.50  5  5   20    5
#> Porsche 914-2       130.0  20  601.5  455 22.15 10.700  83.50  0  5   25   10
#> Lotus Europa        152.0  20  475.5  565 18.85  7.565  84.50  5  5   25   10
#> Ford Pantera L       79.0  40 1755.0 1320 21.10 15.850  72.50  0  5   25   20
#> Ferrari Dino         98.5  30  725.0  875 18.10 13.850  77.50  0  5   25   30
#> Maserati Bora        75.0  40 1505.0 1675 17.70 17.850  73.00  0  5   25   40
#> Volvo 142E          107.0  20  605.0  545 20.55 13.900  93.00  5  5   20   10mtcars %c*% mtcars  # Significantly faster than mtcars * mtcars
#>                         mpg cyl      disp     hp    drat        wt     qsec vs
#> Mazda RX4            441.00  36  25600.00  12100 15.2100  6.864400 270.9316  0
#> Mazda RX4 Wag        441.00  36  25600.00  12100 15.2100  8.265625 289.6804  0
#> Datsun 710           519.84  16  11664.00   8649 14.8225  5.382400 346.3321  1
#> Hornet 4 Drive       457.96  36  66564.00  12100  9.4864 10.336225 377.9136  1
#> Hornet Sportabout    349.69  64 129600.00  30625  9.9225 11.833600 289.6804  0
#> Valiant              327.61  36  50625.00  11025  7.6176 11.971600 408.8484  1
#> Duster 360           204.49  64 129600.00  60025 10.3041 12.744900 250.9056  0
#> Merc 240D            595.36  16  21520.89   3844 13.6161 10.176100 400.0000  1
#> Merc 230             519.84  16  19824.64   9025 15.3664  9.922500 524.4100  1
#> Merc 280             368.64  36  28089.76  15129 15.3664 11.833600 334.8900  1
#> Merc 280C            316.84  36  28089.76  15129 15.3664 11.833600 357.2100  1
#> Merc 450SE           268.96  64  76065.64  32400  9.4249 16.564900 302.7600  0
#> Merc 450SL           299.29  64  76065.64  32400  9.4249 13.912900 309.7600  0
#> Merc 450SLC          231.04  64  76065.64  32400  9.4249 14.288400 324.0000  0
#> Cadillac Fleetwood   108.16  64 222784.00  42025  8.5849 27.562500 323.2804  0
#> Lincoln Continental  108.16  64 211600.00  46225  9.0000 29.419776 317.5524  0
#> Chrysler Imperial    216.09  64 193600.00  52900 10.4329 28.569025 303.4564  0
#> Fiat 128            1049.76  16   6193.69   4356 16.6464  4.840000 379.0809  1
#> Honda Civic          924.16  16   5730.49   2704 24.3049  2.608225 342.9904  1
#> Toyota Corolla      1149.21  16   5055.21   4225 17.8084  3.367225 396.0100  1
#> Toyota Corona        462.25  16  14424.01   9409 13.6900  6.076225 400.4001  1
#> Dodge Challenger     240.25  64 101124.00  22500  7.6176 12.390400 284.5969  0
#> AMC Javelin          231.04  64  92416.00  22500  9.9225 11.799225 299.2900  0
#> Camaro Z28           176.89  64 122500.00  60025 13.9129 14.745600 237.4681  0
#> Pontiac Firebird     368.64  64 160000.00  30625  9.4864 14.784025 290.7025  0
#> Fiat X1-9            745.29  16   6241.00   4356 16.6464  3.744225 357.2100  1
#> Porsche 914-2        676.00  16  14472.09   8281 19.6249  4.579600 278.8900  0
#> Lotus Europa         924.16  16   9044.01  12769 14.2129  2.289169 285.6100  1
#> Ford Pantera L       249.64  64 123201.00  69696 17.8084 10.048900 210.2500  0
#> Ferrari Dino         388.09  36  21025.00  30625 13.1044  7.672900 240.2500  0
#> Maserati Bora        225.00  64  90601.00 112225 12.5316 12.744900 213.1600  0
#> Volvo 142E           457.96  16  14641.00  11881 16.8921  7.728400 345.9600  1
#>                     am gear carb
#> Mazda RX4            1   16   16
#> Mazda RX4 Wag        1   16   16
#> Datsun 710           1   16    1
#> Hornet 4 Drive       0    9    1
#> Hornet Sportabout    0    9    4
#> Valiant              0    9    1
#> Duster 360           0    9   16
#> Merc 240D            0   16    4
#> Merc 230             0   16    4
#> Merc 280             0   16   16
#> Merc 280C            0   16   16
#> Merc 450SE           0    9    9
#> Merc 450SL           0    9    9
#> Merc 450SLC          0    9    9
#> Cadillac Fleetwood   0    9   16
#> Lincoln Continental  0    9   16
#> Chrysler Imperial    0    9   16
#> Fiat 128             1   16    1
#> Honda Civic          1   16    4
#> Toyota Corolla       1   16    1
#> Toyota Corona        0    9    1
#> Dodge Challenger     0    9    4
#> AMC Javelin          0    9    4
#> Camaro Z28           0    9   16
#> Pontiac Firebird     0    9    4
#> Fiat X1-9            1   16    1
#> Porsche 914-2        1   25    4
#> Lotus Europa         1   25    4
#> Ford Pantera L       1   25   16
#> Ferrari Dino         1   25   36
#> Maserati Bora        1   25   64
#> Volvo 142E           1   16    4
## Using matrices
X <- qM(mtcars)
X %cr% v
#>                     mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4             6   6    6  6    6  6    6  6  6    6    6
#> Mazda RX4 Wag         6   6    6  6    6  6    6  6  6    6    6
#> Datsun 710            4   4    4  4    4  4    4  4  4    4    4
#> Hornet 4 Drive        6   6    6  6    6  6    6  6  6    6    6
#> Hornet Sportabout     8   8    8  8    8  8    8  8  8    8    8
#> Valiant               6   6    6  6    6  6    6  6  6    6    6
#> Duster 360            8   8    8  8    8  8    8  8  8    8    8
#> Merc 240D             4   4    4  4    4  4    4  4  4    4    4
#> Merc 230              4   4    4  4    4  4    4  4  4    4    4
#> Merc 280              6   6    6  6    6  6    6  6  6    6    6
#> Merc 280C             6   6    6  6    6  6    6  6  6    6    6
#> Merc 450SE            8   8    8  8    8  8    8  8  8    8    8
#> Merc 450SL            8   8    8  8    8  8    8  8  8    8    8
#> Merc 450SLC           8   8    8  8    8  8    8  8  8    8    8
#> Cadillac Fleetwood    8   8    8  8    8  8    8  8  8    8    8
#> Lincoln Continental   8   8    8  8    8  8    8  8  8    8    8
#> Chrysler Imperial     8   8    8  8    8  8    8  8  8    8    8
#> Fiat 128              4   4    4  4    4  4    4  4  4    4    4
#> Honda Civic           4   4    4  4    4  4    4  4  4    4    4
#> Toyota Corolla        4   4    4  4    4  4    4  4  4    4    4
#> Toyota Corona         4   4    4  4    4  4    4  4  4    4    4
#> Dodge Challenger      8   8    8  8    8  8    8  8  8    8    8
#> AMC Javelin           8   8    8  8    8  8    8  8  8    8    8
#> Camaro Z28            8   8    8  8    8  8    8  8  8    8    8
#> Pontiac Firebird      8   8    8  8    8  8    8  8  8    8    8
#> Fiat X1-9             4   4    4  4    4  4    4  4  4    4    4
#> Porsche 914-2         4   4    4  4    4  4    4  4  4    4    4
#> Lotus Europa          4   4    4  4    4  4    4  4  4    4    4
#> Ford Pantera L        8   8    8  8    8  8    8  8  8    8    8
#> Ferrari Dino          6   6    6  6    6  6    6  6  6    6    6
#> Maserati Bora         8   8    8  8    8  8    8  8  8    8    8
#> Volvo 142E            4   4    4  4    4  4    4  4  4    4    4X %c-% v
#>                      mpg cyl  disp  hp  drat     wt  qsec vs am gear carb
#> Mazda RX4           15.0   0 154.0 104 -2.10 -3.380 10.46 -6 -5   -2   -2
#> Mazda RX4 Wag       15.0   0 154.0 104 -2.10 -3.125 11.02 -6 -5   -2   -2
#> Datsun 710          18.8   0 104.0  89 -0.15 -1.680 14.61 -3 -3    0   -3
#> Hornet 4 Drive      15.4   0 252.0 104 -2.92 -2.785 13.44 -5 -6   -3   -5
#> Hornet Sportabout   10.7   0 352.0 167 -4.85 -4.560  9.02 -8 -8   -5   -6
#> Valiant             12.1   0 219.0  99 -3.24 -2.540 14.22 -5 -6   -3   -5
#> Duster 360           6.3   0 352.0 237 -4.79 -4.430  7.84 -8 -8   -5   -4
#> Merc 240D           20.4   0 142.7  58 -0.31 -0.810 16.00 -3 -4    0   -2
#> Merc 230            18.8   0 136.8  91 -0.08 -0.850 18.90 -3 -4    0   -2
#> Merc 280            13.2   0 161.6 117 -2.08 -2.560 12.30 -5 -6   -2   -2
#> Merc 280C           11.8   0 161.6 117 -2.08 -2.560 12.90 -5 -6   -2   -2
#> Merc 450SE           8.4   0 267.8 172 -4.93 -3.930  9.40 -8 -8   -5   -5
#> Merc 450SL           9.3   0 267.8 172 -4.93 -4.270  9.60 -8 -8   -5   -5
#> Merc 450SLC          7.2   0 267.8 172 -4.93 -4.220 10.00 -8 -8   -5   -5
#> Cadillac Fleetwood   2.4   0 464.0 197 -5.07 -2.750  9.98 -8 -8   -5   -4
#> Lincoln Continental  2.4   0 452.0 207 -5.00 -2.576  9.82 -8 -8   -5   -4
#> Chrysler Imperial    6.7   0 432.0 222 -4.77 -2.655  9.42 -8 -8   -5   -4
#> Fiat 128            28.4   0  74.7  62  0.08 -1.800 15.47 -3 -3    0   -3
#> Honda Civic         26.4   0  71.7  48  0.93 -2.385 14.52 -3 -3    0   -2
#> Toyota Corolla      29.9   0  67.1  61  0.22 -2.165 15.90 -3 -3    0   -3
#> Toyota Corona       17.5   0 116.1  93 -0.30 -1.535 16.01 -3 -4   -1   -3
#> Dodge Challenger     7.5   0 310.0 142 -5.24 -4.480  8.87 -8 -8   -5   -6
#> AMC Javelin          7.2   0 296.0 142 -4.85 -4.565  9.30 -8 -8   -5   -6
#> Camaro Z28           5.3   0 342.0 237 -4.27 -4.160  7.41 -8 -8   -5   -4
#> Pontiac Firebird    11.2   0 392.0 167 -4.92 -4.155  9.05 -8 -8   -5   -6
#> Fiat X1-9           23.3   0  75.0  62  0.08 -2.065 14.90 -3 -3    0   -3
#> Porsche 914-2       22.0   0 116.3  87  0.43 -1.860 12.70 -4 -3    1   -2
#> Lotus Europa        26.4   0  91.1 109 -0.23 -2.487 12.90 -3 -3    1   -2
#> Ford Pantera L       7.8   0 343.0 256 -3.78 -4.830  6.50 -8 -7   -3   -4
#> Ferrari Dino        13.7   0 139.0 169 -2.38 -3.230  9.50 -6 -5   -1    0
#> Maserati Bora        7.0   0 293.0 327 -4.46 -4.430  6.60 -8 -7   -3    0
#> Volvo 142E          17.4   0 117.0 105  0.11 -1.220 14.60 -3 -3    0   -2X %r-% dapply(X, quantile, 0.28)
#>                        mpg cyl    disp     hp    drat     wt    qsec vs am gear
#> Mazda RX4            5.296   2  25.536   7.56  0.7724 -0.102 -0.5216  0  1    1
#> Mazda RX4 Wag        5.296   2  25.536   7.56  0.7724  0.153  0.0384  0  1    1
#> Datsun 710           7.096   0 -26.464  -9.44  0.7224 -0.402  1.6284  1  1    1
#> Hornet 4 Drive       5.696   2 123.536   7.56 -0.0476  0.493  2.4584  1  0    0
#> Hornet Sportabout    2.996   4 225.536  72.56  0.0224  0.718  0.0384  0  0    0
#> Valiant              2.396   2  90.536   2.56 -0.3676  0.738  3.2384  1  0    0
#> Duster 360          -1.404   4 225.536 142.56  0.0824  0.848 -1.1416  0  0    0
#> Merc 240D            8.696   0  12.236 -40.44  0.5624  0.468  3.0184  1  0    1
#> Merc 230             7.096   0   6.336  -7.44  0.7924  0.428  5.9184  1  0    1
#> Merc 280             3.496   2  33.136  20.56  0.7924  0.718  1.3184  1  0    1
#> Merc 280C            2.096   2  33.136  20.56  0.7924  0.718  1.9184  1  0    1
#> Merc 450SE           0.696   4 141.336  77.56 -0.0576  1.348  0.4184  0  0    0
#> Merc 450SL           1.596   4 141.336  77.56 -0.0576  1.008  0.6184  0  0    0
#> Merc 450SLC         -0.504   4 141.336  77.56 -0.0576  1.058  1.0184  0  0    0
#> Cadillac Fleetwood  -5.304   4 337.536 102.56 -0.1976  2.528  0.9984  0  0    0
#> Lincoln Continental -5.304   4 325.536 112.56 -0.1276  2.702  0.8384  0  0    0
#> Chrysler Imperial   -1.004   4 305.536 127.56  0.1024  2.623  0.4384  0  0    0
#> Fiat 128            16.696   0 -55.764 -36.44  0.9524 -0.522  2.4884  1  1    1
#> Honda Civic         14.696   0 -58.764 -50.44  1.8024 -1.107  1.5384  1  1    1
#> Toyota Corolla      18.196   0 -63.364 -37.44  1.0924 -0.887  2.9184  1  1    1
#> Toyota Corona        5.796   0 -14.364  -5.44  0.5724 -0.257  3.0284  1  0    0
#> Dodge Challenger    -0.204   4 183.536  47.56 -0.3676  0.798 -0.1116  0  0    0
#> AMC Javelin         -0.504   4 169.536  47.56  0.0224  0.713  0.3184  0  0    0
#> Camaro Z28          -2.404   4 215.536 142.56  0.6024  1.118 -1.5716  0  0    0
#> Pontiac Firebird     3.496   4 265.536  72.56 -0.0476  1.123  0.0684  0  0    0
#> Fiat X1-9           11.596   0 -55.464 -36.44  0.9524 -0.787  1.9184  1  1    1
#> Porsche 914-2       10.296   0 -14.164 -11.44  1.3024 -0.582 -0.2816  0  1    2
#> Lotus Europa        14.696   0 -39.364  10.56  0.6424 -1.209 -0.0816  1  1    2
#> Ford Pantera L       0.096   4 216.536 161.56  1.0924  0.448 -2.4816  0  1    2
#> Ferrari Dino         3.996   2  10.536  72.56  0.4924  0.048 -1.4816  0  1    2
#> Maserati Bora       -0.704   4 166.536 232.56  0.4124  0.848 -2.3816  0  1    2
#> Volvo 142E           5.696   0 -13.464   6.56  0.9824  0.058  1.6184  1  1    1
#>                     carb
#> Mazda RX4              2
#> Mazda RX4 Wag          2
#> Datsun 710            -1
#> Hornet 4 Drive        -1
#> Valiant               -1
#> Duster 360             2
#> Merc 240D              0
#> Merc 230               0
#> Merc 280               2
#> Merc 280C              2
#> Merc 450SE             1
#> Merc 450SL             1
#> Merc 450SLC            1
#> Lincoln Continental    2
#> Chrysler Imperial      2
#> Fiat 128              -1
#> Honda Civic            0
#> Toyota Corolla        -1
#> Toyota Corona         -1
#> Dodge Challenger       0
#> AMC Javelin            0
#> Camaro Z28             2
#> Pontiac Firebird       0
#> Fiat X1-9             -1
#> Porsche 914-2          0
#> Lotus Europa           0
#> Ford Pantera L         2
#> Ferrari Dino           4
#> Maserati Bora          6
#> Volvo 142E             0# \donttest{
## Chained Operations
library(magrittr)
#>
#> Attaching package: ‘magrittr’#> The following objects are masked from ‘package:testthat’:
#>
#>     equals, is_less_than, notmtcars %>% fwithin %r-% rnorm(11) %c*% 5 %>%
tfm(mpg = fsum(mpg)) %>% qsu
#>        N     Mean        SD        Min        Max
#> mpg   32  224.007         0    224.007    224.007
#> cyl   32  -1.2766    8.9296   -12.2141     7.7859
#> disp  32  12.1863  619.6935  -785.9231  1218.5769
#> hp    32   0.0279  342.8143  -473.4096   941.5904
#> drat  32  -3.1078    2.6734    -7.2906     3.5594
#> wt    32  -5.7421    4.8923   -14.2633     5.2917
#> qsec  32   9.1091    8.9347    -7.6347    34.3653
#> vs    32   1.2366    2.5201    -0.9509     4.0491
#> am    32    1.221     2.495    -0.8103     4.1897
#> gear  32   1.4135     3.689     -2.024      7.976
#> carb  32   2.7685     8.076     -6.294     28.706# }