R/sample_variance.R

#' Sample Variance
#'
#' This function takes a single numeric vector and returns the variance of the vectors values.
#'
#' @param x Numeric vector of values
#'
#' @return Sample variance
#' @export
#'
#' @examples
#' x <- 1:10
#' sample_variance(x)
#'
#' y <- rnorm(100)
#' sample_variance(y)
sample_variance <- function(x) {

  check_numeric_vector(x)  # Check that the input is a single numeric vector

  x_bar <- sample_mean(x)  # Get the sample mean
  n <- length(x)  # Number of elements in x
  total <- 0

  for (i in 1:n) {

    total <- total + (x[i]-x_bar)^2

  }

  return(total / (n-1))

}
AshleyDennisHenderson/data.summary documentation built on June 12, 2019, 11:25 a.m.