8 mop()
Essentially, mop is a wrapper for sweep(x, MARGIN, apply(…), FUN). This function is useful for indexing variables by their means, for example, so that the magnitude of a value relative to its average is known.
The four required arguments are x
, m
, s
, and f
–the collection (e.g. matrix), margin (1 for row-wise or 2 for column-wise), summary statistic function, and binary opertaor function, respectively. A fifth, optional argument ...
passes to sweep()
. The output is typically a matrix or dataframe, depending on the inputs and functions being passed.
8.0.1 Mopping a Dataframe
## mpg cyl disp hp drat wt
## Mazda RX4 1.0452636 0.9696970 0.6934756 0.7498935 1.0843688 0.8143601
## Mazda RX4 Wag 1.0452636 0.9696970 0.6934756 0.7498935 1.0843688 0.8936203
## Datsun 710 1.1348577 0.6464646 0.4680961 0.6340009 1.0704666 0.7211128
## Hornet 4 Drive 1.0651734 0.9696970 1.1182295 0.7498935 0.8563733 0.9993006
## Hornet Sportabout 0.9307824 1.2929293 1.5603202 1.1930124 0.8758363 1.0692361
## Valiant 0.9009177 0.9696970 0.9752001 0.7158074 0.7673994 1.0754526
## qsec vs am gear carb
## Mazda RX4 0.9221934 0.000000 2.461538 1.0847458 1.4222222
## Mazda RX4 Wag 0.9535682 0.000000 2.461538 1.0847458 1.4222222
## Datsun 710 1.0426500 2.285714 2.461538 1.0847458 0.3555556
## Hornet 4 Drive 1.0891519 2.285714 0.000000 0.8135593 0.3555556
## Hornet Sportabout 0.9535682 0.000000 0.000000 0.8135593 0.7111111
## Valiant 1.1328524 2.285714 0.000000 0.8135593 0.3555556
8.1 mop_div()
The function mop_div simplifies mop by operating only on columns and assuming f to be the division operator.
Only two parameters are reuqired: the collection x
and summary statistic function s
. The output is similar to that of mop()
.
8.1.1 Indexing a Dataframe
## mpg cyl disp hp drat wt
## Mazda RX4 1.0452636 0.9696970 0.6934756 0.7498935 1.0843688 0.8143601
## Mazda RX4 Wag 1.0452636 0.9696970 0.6934756 0.7498935 1.0843688 0.8936203
## Datsun 710 1.1348577 0.6464646 0.4680961 0.6340009 1.0704666 0.7211128
## Hornet 4 Drive 1.0651734 0.9696970 1.1182295 0.7498935 0.8563733 0.9993006
## Hornet Sportabout 0.9307824 1.2929293 1.5603202 1.1930124 0.8758363 1.0692361
## Valiant 0.9009177 0.9696970 0.9752001 0.7158074 0.7673994 1.0754526
## qsec vs am gear carb
## Mazda RX4 0.9221934 0.000000 2.461538 1.0847458 1.4222222
## Mazda RX4 Wag 0.9535682 0.000000 2.461538 1.0847458 1.4222222
## Datsun 710 1.0426500 2.285714 2.461538 1.0847458 0.3555556
## Hornet 4 Drive 1.0891519 2.285714 0.000000 0.8135593 0.3555556
## Hornet Sportabout 0.9535682 0.000000 0.000000 0.8135593 0.7111111
## Valiant 1.1328524 2.285714 0.000000 0.8135593 0.3555556
8.2 smop()
The function smop()
simplifies mop()
by operating only on columns–this is more general than mop_div()
in which it operates on columns and only uses the division binary operator.
The three required inputs are the collection x
, summary statistic function s
, and binary operator function f
. A fourth, optional input ...
passes to mop()
, which passes to sweep()
.
8.2.1 Indexing a Dataframe, Part 2
## mpg cyl disp hp drat wt
## Mazda RX4 1.0452636 0.9696970 0.6934756 0.7498935 1.0843688 0.8143601
## Mazda RX4 Wag 1.0452636 0.9696970 0.6934756 0.7498935 1.0843688 0.8936203
## Datsun 710 1.1348577 0.6464646 0.4680961 0.6340009 1.0704666 0.7211128
## Hornet 4 Drive 1.0651734 0.9696970 1.1182295 0.7498935 0.8563733 0.9993006
## Hornet Sportabout 0.9307824 1.2929293 1.5603202 1.1930124 0.8758363 1.0692361
## Valiant 0.9009177 0.9696970 0.9752001 0.7158074 0.7673994 1.0754526
## qsec vs am gear carb
## Mazda RX4 0.9221934 0.000000 2.461538 1.0847458 1.4222222
## Mazda RX4 Wag 0.9535682 0.000000 2.461538 1.0847458 1.4222222
## Datsun 710 1.0426500 2.285714 2.461538 1.0847458 0.3555556
## Hornet 4 Drive 1.0891519 2.285714 0.000000 0.8135593 0.3555556
## Hornet Sportabout 0.9535682 0.000000 0.000000 0.8135593 0.7111111
## Valiant 1.1328524 2.285714 0.000000 0.8135593 0.3555556