What does "trim" mean in "apply()"?

r

Solution

The `trim` argument is not to do with `apply`, but to do with `mean`.

From `?mean`:

`trim`: the fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed. Values of trim outside that range are taken as the nearest endpoint.

`sd` doesn't have an argument named `trim`, so if you tried `apply(x, 2, sd, trim = .2)` you would get an error about trying to use an unused argument.

This is not an argument to `apply`, but it is an argument passed down from `apply` to `mean` using `...`:

From `?apply`:

`...` : optional arguments to `FUN`.

This adds flexibility and reduces dependence on creating anonymous functions. For example, without the `...` argument, if you wanted to do a trimmed mean, you would have to do:

apply(x, 2, function(x) mean(x, trim = .2))

Problem

What does the argument "trim" mean in "apply()"? For example, I would like to calculate the mean of each column of an array `x`: ``` apply(x, 2, mean, trim = .2) ``` How is it different from ``` apply(x, 2, mean) ``` Which one shall I use? If I would like to calculate the standard deviation of each column of an array `x`, which one shall I use ``` apply(x, 2, sd, trim = .2) ``` or ``` apply(x, 2, sd) ``` Thanks!

Original source