| top_perc | R Documentation |
Selects the top (largest) or bottom (smallest) percentage of data based on specified traits. Positive percentages extract the largest values; negative percentages extract the smallest values.
top_perc(
data,
perc,
cols,
by = NULL,
keep_data = FALSE,
stats = c("n", "min", "max", "mean", "median", "sd", "se", "cv")
)
data |
A data.frame or data.table. |
perc |
A numeric vector strictly between -1 and 1 (excluding 0). Positive values (e.g., 0.05) select the top X% of largest values. Negative values (e.g., -0.1) select the bottom X% of smallest values. |
cols |
Numeric trait column(s) to analyse, as names or indices. |
by |
Grouping column(s), as names or indices. Default is NULL. |
keep_data |
Logical. If TRUE, returns a named list where each element
contains both |
stats |
Statistics of the selected records, computed exactly as in
|
Ranking uses ties.method = "min", so tied values at the cut-off are
all selected and slightly more than the requested share can be returned.
Missing values are removed before ranking. Groups with fewer than
1 / abs(perc) non-missing records cannot contribute a single
record: they are kept in the summary with n = 0 and a warning is
issued.
keep_data = FALSE: a data.frame with one row per
by / cols / perc combination: the by
columns, variable, the requested stats and
selection.
keep_data = TRUE: a named list (one element per perc
value) where each element is a list with $stat and $data.
desc_stats() for statistics of all records.
# Example 1: Basic usage with single trait
# This example selects the top 10% of observations based on Petal.Width
# keep_data=TRUE returns both summary statistics and the filtered data
top_perc(iris,
perc = 0.1, # Select top 10%
cols = c("Petal.Width"), # Column to analyze
keep_data = TRUE) # Return both stats and filtered data
# Example 2: Using grouping with 'by' parameter
# This example performs the same analysis but separately for each Species
# Returns a data.frame of summary statistics, one row per Species
top_perc(iris,
perc = 0.1, # Select top 10%
cols = c("Petal.Width"), # Column to analyze
by = "Species") # Group by Species
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.