basic_stats: Calculate basic statistics for a numeric vector

View source: R/basic_stats.R

basic_statsR Documentation

Calculate basic statistics for a numeric vector

Description

Calculate basic statistics (e.g. mean, min, max, sd, IQR) from a numeric vector.

Usage

basic_stats(
  x,
  f = list(min = min, mean = mean, median = stats::median, max = max, sd = stats::sd, IQR
    = stats::IQR, MAD = stats::mad),
  p = function(x) round(x, digits = 2),
  output = "wdf",
  ...
)

Arguments

x

A numeric vector for which to calculate statistics.

f

A named list of functions which are used to calculate statistics.

p

A function for processing summary statistics. The default is a function which rounds each output to the nearest two decimal places. p = NULL leaves summary statistics unchanged.

output

A character specifying the format in which summary statistics should be returned. The currently implemented options are: (1) "vec" a numeric vector; (2) "ldf" a long-format dataframe in long format; and (3) "wdf", a wide-format dataframe. The default option is "wdf".

...

Other arguments that apply to all functions in f, such as na.rm = TRUE.

Value

Basic statistics for a vector of numbers, either in the format of a numeric vector, a long-format dataframe or a wide-format dataframe depending on the input to output (see above).

Author(s)

Edward Lavender

Examples


#### Define a vector:
x <- runif(10, 0, 10)

#### Example (1): Numeric vector output
basic_stats(x, output = "vec")

#### Example (2): Long-format dataframe output
basic_stats(x, output = "ldf")

#### Example (3): Wide-format dataframe output
basic_stats(x, output = "wdf")

#### Example (4): Functions can be adjusted in f argument by supplying a named list:
basic_stats(x,
            f = list(median = median,
                     quantile25 = function(x) { quantile(x, prob = 0.25) }),
            output = "ldf"
            )

#### Example (5) Save typing by passing any arguments that apply to all functions via ...
basic_stats(c(x, NA), f = list(mean = mean, min = min), na.rm = TRUE)

#### Example (6) Unsupported outputs return a warning and the function defaults to 'ldf'
## Not run: 
basic_stats(1:10, output = "df")

## End(Not run)


edwardlavender/utils.add documentation built on Dec. 14, 2024, 8:11 a.m.