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 - 10:39:55 - TheresaScott
Main.DesiredOutputFn moved from Main.Function2 on 10 Nov 2006 - 16:42 by TheresaScott - put it back
 
Register | Log In
Copyright © 2009 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback