mean_sd: Calculate mean +/- sd or other parameters with appropriate...

View source: R/mean_sd.R

mean_sdR Documentation

Calculate mean +/- sd or other parameters with appropriate sig figs

Description

mean_sd takes as input either a) a numeric vector or b) a number for the mean and a second number for the sd, and it outputs the mean plus or minus the standard deviation of that vector with an appropriate number of sig figs. If a vector was supplied, optionally also obtain the median, range, CV, and/or 95

Usage

mean_sd(
  x,
  stdev.x = NULL,
  calcRange = FALSE,
  numDigRange = NA,
  calcCV = FALSE,
  calcMedian = FALSE,
  calc95CI = FALSE,
  reportn = FALSE,
  ndig = NA,
  na.rm = TRUE
)

Arguments

x

A vector of numbers or a single mean

stdev.x

The sd when you want to provide it (leave as NULL if providing a vector of numbers)

calcRange

Should the range be reported? (logical) (TRUE or FALSE)

numDigRange

Number of digits to report for the range

calcCV

Should the coefficient of variation be reported? (logical)

calcMedian

Should the median be reported? (logical)

calc95CI

Should the 95% confidence interval be reported? (logical) If calc95CI is set to TRUE, it will return the 95% confidence interval as calculated as mean(x) +/- tn-1, 1-alpha/2 * sd(x)/sqrt(n)

reportn

Should the number of observations be reported? (logical)

ndig

Optionally set the number of sig figs to use if you don't want that calculated automatically

na.rm

Should NA values be removed when calculating the mean, standard deviation, etc.? (logical) They'll still be included in the count of observations if reportn is TRUE.

Details

Example of ultimate output with all possible options set to TRUE:

5.1 (5) +/- 0.2 (4.7 to 5.3, 3.7%, 95%CI: 4.6 to 5.2, n = 18)

mean (median) +/- sd (range.min to range.max, CV%, 95%CI: lower to upper, n = n).

A few important notes:

  • I have not set this up to deal with mean values in scientific notation.

  • If sd(x) == 0, output will be unique(x).

  • If you want to supply the mean and standard deviation and have this function return that with appropriate sig figs, you cannot supply a vector of values for x and stdev.x. That does not work. If x is a vector of length > 1, the function will calculate mean, standard deviation, etc. rather than taking those values from a second vector. It will result in output, just not the correct output.

Value

Returns a character string

Examples

mean_sd(rnorm(10, 5, 1))
mean_sd(rnorm(10, 5, 1), calcRange = TRUE)
mean_sd(rnorm(10, 5, 1), calcCV = TRUE)
mean_sd(rnorm(10, 5, 1), calcMedian = TRUE)
mean_sd(rnorm(10, 5, 1), calc95CI = TRUE)
mean_sd(rnorm(10, 5, 1), reportn = TRUE)
mean_sd(rnorm(10, 5, 1), calcRange = TRUE, calcCV = TRUE, reportn = TRUE)


shirewoman2/LaurasHelpers documentation built on Oct. 22, 2023, 2:07 p.m.