[R] How does this function print, why is n1 which equals 1 printed as 2?
Duncan Murdoch
murdoch.duncan at gmail.com
Fri Feb 1 15:02:03 CET 2013
On 13-02-01 8:47 AM, John Sorkin wrote:
> 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.
I haven't run the code, but presumably it's just the usual auto
printing. Unless a function sets the result to be invisible, results
of functions are printed by calling print() after they are returned.
Since your function returns something with class
c("n.for.2means", "list")
R will look for a print method for those classes, and use it to print
the result. There's no print.list method in standard R; it just uses
the default. But there's probably a print.n.for.2means function in the
package (possibly not exported; you might need
epicalc:::print.n.for.2means to see it). There might be a print.list
function there instead or as well.
Duncan Murdoch
> 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.
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list