Elaborating a little longer

However, we could elaborate a little longer to get a more detailed summary that will also output the alternative hypothesis and the confidence interval. The following code block demonstrates how such a function can be created. Afterward, we will see how to keep this function close in order to easily call and use it later:

z.test <- function(sample, mu, sigma, conf.lvl = .95,                   alternative = 'two.sided'){  n <- length(sample)  xbar <- mean(sample, na.rm =T)  z <- (xbar - mu)/(sigma/sqrt(n))  if(alternative == 'two.sided'){    p.value <- 2*pnorm(-abs(z))    alt <- 'not equal '    err <- -qnorm((1-conf.lvl)/2)*sigma/sqrt(n)    a <- xbar - err    b <- xbar + err  }  else if(alternative == 'greater'){    p.value <- pnorm(z, lower.tail = F)

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.