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!
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 toFUN
.
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))
Just tried this with dplyr and it seems to work fine.
df <- x %>%
select(V1:V7) %>%
summarise_each(funs(mean(., na.rm=T, trim=0.2)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.