Description Usage Arguments Details Value Examples
trim
returns a trimmed numeric vector.
1 2 |
x |
A numeric vector. |
method |
A character string indicating the desired method of trimming
with |
lo |
The lower value/percentile for trimming. See Details for more information. |
hi |
The upper value/percentile for trimming. See Details for more information. |
replace |
Either |
trim
returns a trimmed version of the numeric vector x
.
NA
values in x
are ignored during the trimming process
but are preserved in the output. trim
will do one-sided trimming
if only one lo
or hi
argument is provided
(e.g. trim(x, lo=-1)
will trim x
at a lower value of -1).
trim
is designed to be readable from the function call. For example:
trim(x, "value", lo=-1, hi=1)
can be read as
"Trim x at -1 and 1".
trim(x, "percentile", lo=.05, hi=.95)
can be read as
"Trim x at the 5th and 95th percentiles".
The arguments lo
and hi
are used based on method
.
trim
offers several different options for method
:
value: lo
and hi
are used as raw values
(e.g. .05 is the value .05).
percentile: lo
and hi
are used as percentiles
(e.g. .05 is 5th percentile).
The output of trim
is a trimmed numeric vector with the same
length as x
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | set.seed(1337)
x <- rnorm(1e4)
summary(x)
#####
# Common use cases
#
# I want to trim at the values -1 and 1!
x_val <- trim(x, lo=-1, hi=1)
summary(x_val)
# I want to trim at 5th and 95th percentiles!
x_per <- trim(x, "percentile", lo=.05, hi=.95)
summary(x_per)
# I only want to trim values above the 95th percentile!
x_hi <- trim(x, "percentile", hi=.95)
summary(x_hi)
# I want the trimmed values converted to NAs!
x_NA <- trim(x, "percentile", lo=.05, hi=.95, replace=NA)
summary(x_NA)
# I want to trim negative values to -1! (Weird but okay!)
x_neg <- trim(x, lo=0, replace=-1)
summary(x_neg)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.