Skip to contents

Sets the values in a numeric vector that are beyond the specified quantiles to NA, based on the cut parameter. Values below the cut quantile and above the 1 - cut quantile become NA; the length of x is unchanged, which is the useful behaviour inside dplyr::mutate().

Usage

trim(x, cut)

Arguments

x

A numeric vector to be trimmed.

cut

The proportion of data to be trimmed from both ends of the distribution. For example, a cut of 0.05 will remove the lowest and highest 5% of the data. Must be in [0, 0.5].

Value

A numeric vector the same length as x, with the values outside the cut and 1 - cut quantiles set to NA.

Examples

set.seed(123)
data <- rnorm(100)
trimmed_data <- trim(x = data, cut = 0.05)