R/myseq_n.R

Defines functions myseq_n

Documented in myseq_n

#' "myseq_n" Function Execution
#'
#' @param x numeric
#' @param n numeric
#'
#' @return
#' @export myseq_n
#'
#' @examples
myseq_n <- function(x,n) {
  # error checks
  if(!is.numeric(n)){
    stop("n needs to be numeric")
  }
  if(!is.numeric(x)){
    stop("x needs to be numeric")
  }
  if(length(x) != 3){
    stop("x must be length 3")
  }
  if(n < 0){
    stop("n must be greater than 0")
  }
  # scenarios start
  # scenario 1: 1 < n < = length(x)
  if ((n >= 1)  & (n <= length(x) )){
    return (x[n])
  }
  # scenario 2: n < 1
  else if (n < 1){
    stop("n needs to be greater than zero")
  }
  # scenario 3: n > length(x)
  else {
    x_out <- vector(length = n)
    x_out[1:3] <- x
    for(i in 4:n){
      x_out[i] = x_out[i-1] + ((x_out[i-3] - x_out[i-2]) /i)
    }
    return (x_out[n])
  }
}
21Sp-STAT-413-613/hw04prebeccalipton documentation built on March 1, 2021, 12:01 a.m.