# [R] Multiple outputs of a function

Mark Myatt mark at myatt.demon.co.uk
Fri Oct 20 17:12:05 CEST 2000

```isabelle Zabalza-Mezghani <isabelle.zabalza-mezghani at ifp.fr> writes:

>    Hello everybody,
>
>    I'm writting some functions for experimental designs. The one I'm
>    working on is similar to "fac.design" on Splus.
>    The problem I have is with the form of the output :
>
>    When the argument fraction is submitted, Splus gives something like
>    :
>
>         A  B  C
>     1   -  -  -
>     2   +  +  -
>     3   +  -  +
>     4   -  +  +
>
>    Fraction :  ~A:B:C
>
>    The first part of this output is the design, and the second part is
>    the formula which allows to fractionate the full design. This
>    output isn't a list.
>    My problem is the following :
>    I can't manage to obtain such a composite output. The lonely way
>    I've already found is to use :
>    print(design)
>    cat("Fraction:  ")
>    print(attr(design,"fraction")).
>
>    This solution is only suitable for an output on screen, but it
>    reveals bad as soon as I assign this function
>    to an object (for ex: tmp <- fac.design( ....) ) since only the
>    last command I performed (here print(attr(design,"fraction")))
>    is assigned to my object tmp.
>
>    I hope someone can help me ...

Return a list. For example, here is a simple function to calculate and
return a risk ratio and 95% confidence from a 2-by-2 table

rr22 <- function(exposure, outcome)
{
tab <- table(exposure, outcome)
a <- tab[1,1]; b <- tab[1,2]; c <- tab[2,1]; d <- tab[2,2]
rr <- (a / (a + b)) / (c / (c + d))
se.log.rr <- sqrt((b/a) / (a+b) + (d / c) / (c + d))
lci.rr <- exp(log(rr) - 1.96 * se.log.rr)
uci.rr <- exp(log(rr) + 1.96 * se.log.rr)
list(estimate = rr, conf.int = c(lci.rr, uci.rr))
}

In use:

> rr22(PASTA, ILL)
\$estimate
[1] 1.682692

\$conf.int
[1] 1.255392 2.255433

> b <- rr22(PASTA, ILL)
> b\$estimate
[1] 1.682692
> b\$conf.int
[1] 1.255392 2.255433
> b\$conf.int[1]
[1] 1.255392
> b\$conf.int[2]
[1] 2.255433

If you need the formatted output then you need to declare a class for
the output list and create a print method for that class:

rr22 <- function(exposure, outcome)
{
tab <- table(exposure, outcome)
a <- tab[1,1]; b <- tab[1,2]; c <- tab[2,1]; d <- tab[2,2]
rr <- (a / (a + b)) / (c / (c + d))
se.log.rr <- sqrt((b / a) / (a + b) + (d / c) / (c + d))
lci.rr <- exp(log(rr) - 1.96 * se.log.rr)
uci.rr <- exp(log(rr) + 1.96 * se.log.rr)
rr22.output <- list(estimate = rr, conf.int = c(lci.rr, uci.rr))
class(rr22.output) <- "rr22"
rr22.output
}

print.rr22 <- function(x)
{
cat("RR     : ", x\$estimate, "\n",
"95% CI : ", x\$conf.int[1], "-", x\$conf.int[2], "\n",
sep = "")
}

In use:

> b <- rr22(PASTA,ILL)
> b
RR     : 1.682692
95% CI : 1.255392-2.255433
> b\$estimate
[1] 1.682692
> b\$conf.int
[1] 1.255392 2.255433

I hope that helps.

Mark

--
Mark Myatt

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```