trim: Trim a numeric vector

Description Usage Arguments Details Value Examples

Description

trim returns a trimmed numeric vector.

Usage

1
2
trim(x, method = c("value", "percentile"), lo = NULL, hi = NULL,
  replace = NULL)

Arguments

x

A numeric vector.

method

A character string indicating the desired method of trimming with "value" being the default. This must be (an abbreviation of) one of the strings "value" or "percentile". See Details for more information.

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 NULL ("rounds" values according to the lo and hi arguments), NA, or a single value that will replace the trimmed values. The default is NULL.

Details

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:

The arguments lo and hi are used based on method. trim offers several different options for method:

Value

The output of trim is a trimmed numeric vector with the same length as x.

Examples

 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)

derek-damron/transform documentation built on May 15, 2019, 3:57 a.m.