Skip to contents

From a data frame, returns the top n values from a named column based on the values in another column, and pastes them together in a grammatically correct string. Ideal for commentary which aims to describe e.g. "the top 3 values of X were for the following regions:"

Usage

top_x_percent(
  df,
  n = 1,
  values = "value",
  names = "name",
  sum_percentage = TRUE
)

Arguments

df

a dataframe of value-name pairs.

n

the number of values/names of interest; e.g. n = 3 will return a commentary discussing the top 3 values

values

column name of the column containing numerical values to sort top n values on. Defaults to "value".

names

column name of the column containing name values used to describe the top n values. Defaults to "name"

sum_percentage

logical. Do you want a single percentage of the total to be returned for all n number of values? Defaults to TRUE. If false, returns an individual percentage for each value.

Value

Returns a string of name values for the top n values.