
Robert S. answered 11/22/19
R Developer and Analyst
In my experience, I've used `sweep()` to know how "far away" a particular variable's value is from its mean, for example.
To illustrate, let's say that I wanted to know for every variable's value in the `mtcars` dataset the distance from its mean in terms of percent (i.e., I want to be able to say "Mazda RX4's mpg value is X% larger/smaller than the mean of all cars"). See and execute the code below:
```
mysweep <- sweep(mtcars, 2, apply(mtcars, 2, mean), `/`)
mysweep
```
From the above code block, we are dividing each variable's value by its mean (i.e., x/mean(x)). In other words, we are "indexing" each variable.
In terms how I've personally used the function, I've created a simplification of `sweep()` called `mop()` that takes a summary statistic FUNCTION as an argument instead of a summary statistic VALUE, which can be seen in the following link: https://github.com/robertschnitman/afp/blob/master/R/mop.R
I am willing to bet that others have used `sweep()` for more advanced purposes: I am just relaying my own experience with the function. So, hopefully, this answers your question!