R/MyFunction.R

Defines functions MyFunction

Documented in MyFunction

#' Square a numeric vector.
#'
#' This function takes in a atomic numeric vector that contains more than 3 elements, squares each element, and sum them up
#' @param a A numeric vector that contains more than 3 elements
#' @param b A logical vector True / False to decide if print the sum
#' @return Squared numeric vector
#' @export

MyFunction <- function(a, b) {
  if (length(a) < 3) {
    warning('Please enter a vector that contains more than 3 elements!')
  }
  else {
    sum <- 0
    for (i in 1:length(a)) {
      a[i] <- a[i]^2
      sum <- sum + a[i]
    }
    if (b == TRUE) {
      cat('The sum of all elements in the vector a is: ', sum)
    }
  }
}
Maxixixi/HM5 documentation built on Oct. 30, 2019, 9:22 p.m.