# [R] How does this function print, why is n1 which equals 1 printed as 2?

John Sorkin JSorkin at grecc.umaryland.edu
Fri Feb 1 14:47:16 CET 2013

```Windows 7, R 2.12.1
Colleagues,
I am trying to understand the n.for.2means function. The code below is a copy of the function (renamed to n.for.2means.js). I have inserted a single line of code towards the bottom of the function which uses the cat function to print the value of n1. You will note the value (preceded by stars) is printed as 1.
The function (1) prints a lot of output without any instructions in the function to print anything (other than the cat statement I added), and when it prints (2) reports the value of n1 to be 2!.
I have two questions, (i) how is the function printing when there is no code to print and (ii) how is n1 which equals 1 being reported as 2? I suspect there is something fundamental about R that I don't know.
Thank you for the help.
John

library(epicalc)
n.for.2means.js <- function (mu1, mu2, sd1, sd2, ratio = 1, alpha = 0.05, power = 0.8)
{
n1 <- (sd1^2 + sd2^2/ratio) * (qnorm(1 - alpha/2) - qnorm(1 -  power))^2/(mu1 - mu2)^2
n1 <- round(n1)
n2 <- ratio * n1
if (length(alpha) == 1) {
alpha1 <- NULL
}
else {
alpha1 <- alpha
}
if (length(power) == 1) {
power1 <- NULL
}
else {
power1 <- power
}
if (length(ratio) == 1) {
ratio1 <- NULL
}
else {
ratio1 <- ratio
}
table1 <- cbind(mu1, mu2, sd1, sd2, n1, n2, alpha1, power1,
ratio1)
colnames(table1)[colnames(table1) == "alpha1"] <- "alpha"
colnames(table1)[colnames(table1) == "power1"] <- "power"
colnames(table1)[colnames(table1) == "ratio1"] <- "n2/n1"
table1 <- as.data.frame(table1)
cat("******n1*******=",n1,"\n")
returns <- list(mu1 = mu1, mu2 = mu2, sd1 = sd1, sd2 = sd2,
alpha = alpha, n1 = n1, n2 = n2, power = power, ratio = ratio,
table = table1)
class(returns) <- c("n.for.2means", "list")
returns
}
n.for.2means.js(0,8,10/6,10/6)

John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
Confidentiality Statement:
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information.  Any unauthorized use, disclosure or distribution is prohibited.  If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
```