R/sample_mean.R

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

  if (!is.null(dim(x))) {

    stop('Data is not a single numeric vector')

  }

  if (length(x) == 0) {

    stop('Vector is of length 0')

  }

  if (is.numeric(x) == FALSE) {

    stop('Given data is not numeric')

  }

  n <- length(x)  # Number of elements in x
  total <- 0

  for (i in 1:n){

    total <- total + x[i]

  }

  return(total / n)

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