knitr::opts_chunk$set(echo = TRUE, comment='ws#')

mysd(x)

  1. The function takes an argument 'x' which needs to be a Numeric vector.
mysd <- function(x){
  N        <- length(x)
  variance <- sum((x - mean(x))^2) / (N - 1)
  std      <- sqrt(variance)
}
  1. Run with given input
L <- 1:20
cat(mysd(L))
  1. Compare with builtin sd()
cat("Built in sd():",sd(L), "\nMy function:", mysd(L))
  1. Alter function
mysd <- function(x){
  N        <- length(x)
  variance <- sum((x - mean(x))^2) / (N - 1)
  std      <- sqrt(variance)
  list(sd = std, variance = variance)

}
mysd(x = L)


agracy2246/MATH4753grac0009 documentation built on April 26, 2020, 9:39 a.m.