I would be inclined to write your function like this:
Test <- function(xbar, mu0, s, n, alpha)
# xbar = sample mean
# mu0 = mean of population
# s = standard deviation of sample
# n = sample size
# alpha = alpha risk
{
t <- round(abs(qt(alpha/2, n-1)), digits = 3)
Tstat <- (xbar-mu0)/s*sqrt(n)
cat(
sprintf(
paste0(c("Critical values of t(alpha) = %s\n",
"Test statistics T = %s\n",
"%s\n\n",
collapse = "\n")),
t, # first %s
Tstat, # second %s
if (abs(Tstat) > t) "H0 rejected" else "H0 not rejected" # third %s
),
sep = ""
)
}
Test(57.1, 55, 1.4, 36, 0.01)
some notes:
- In
sprintf, you are building a string. The %s is a placeholder for a string to be added later. The values to be inserted are given as subsequent arguments. I often find this easier to read and understand than a lot of pasteing. (You may also want to see the glue package for similar functionality).
- I changed your
T variable to Tstat. T is, by default, a synonym for TRUE. You can overwrite it, but you might confuse yourself or others sometime in the future. I recommend avoiding it as an object name.
- I changed your phrasing of
H0 accepted to H0 not rejected. Hypothesis tests don't typically confirm a hypothesis. We either have enough evidence on hand to reject it, or we don't.