A function that returns the desired output from another function

Often, you do not want to return all of the default output from a specific function. The following function is just one example of a user-defined function that returns (i.e., prints) only the desired output from a specific function --- in this case, just the correlation and p-value from the Hmisc package's rcorr() function.

library(Hmisc)

myrho <- function (x, y) {
   rhosumm <- rcorr(x, y)
   rho <- round(rhosumm$r[1,2], 3)
   pval <- round(rhosumm$P[1,2], 4)
   c(Rho = rho, p.value = pval)
}

# Example
y <- c(4, 1, 0, 1, 4)
z <- c(1, 2, 3, 4, NA)
myrho(y, z)

This user-defined function could easily extended to return the desired output from several different functions.
Topic revision: r2 - 10 Nov 2006, TheresaScott
 

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback