Stropharia
2008-May-08 12:20 UTC
[R] Re turning variable names with variables (in a function)
Dear R Users, I have written a function that returns 4 variables. I would like to have the variables returned with their variable names, is this possible? ----------------------------- R Code ------------------------- mc.error <- function(T, p=0.05){ se <- sqrt((p)*(1-(p))/T) # standard error upper <- p+(1.96*se) # upper CI lower <- p-(1.96*se) # lower CI cv <- se/p # coefficient of variation results <- c(se, upper, lower, cv) return(results) } ----------------------------- R Code ------------------------- This returns (or something like this, depending on the value of T): [1] 0.004998685 0.059797422 0.040202578 0.099973695 I would like: [1] se=0.004998685 upper=0.059797422 lower=0.040202578 cv=0.099973695 Or even better: se upper lower cv 0.004998685 0.059797422 0.040202578 0.099973695 Any help is much appreciated, thanks. Steve ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Steven Worthington Ph.D. Candidate New York Consortium in Evolutionary Primatology & Department of Anthropology New York University 25 Waverly Place New York, NY 10003 U.S.A. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- View this message in context: http://www.nabble.com/Returning-variable-names-with-variables-%28in-a-function%29-tp17124874p17124874.html Sent from the R help mailing list archive at Nabble.com.
Shubha Vishwanath Karanth
2008-May-08 12:42 UTC
[R] Re turning variable names with variables (in a function)
You can just go for a small change in the function as, results <- cbind(se, upper, lower, cv) OR results <- data.frame(se, upper, lower, cv) -S- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Stropharia Sent: Thursday, May 08, 2008 5:51 PM To: r-help at r-project.org Subject: [R] Re turning variable names with variables (in a function) Dear R Users, I have written a function that returns 4 variables. I would like to have the variables returned with their variable names, is this possible? ----------------------------- R Code ------------------------- mc.error <- function(T, p=0.05){ se <- sqrt((p)*(1-(p))/T) # standard error upper <- p+(1.96*se) # upper CI lower <- p-(1.96*se) # lower CI cv <- se/p # coefficient of variation results <- c(se, upper, lower, cv) return(results) } ----------------------------- R Code ------------------------- This returns (or something like this, depending on the value of T): [1] 0.004998685 0.059797422 0.040202578 0.099973695 I would like: [1] se=0.004998685 upper=0.059797422 lower=0.040202578 cv=0.099973695 Or even better: se upper lower cv 0.004998685 0.059797422 0.040202578 0.099973695 Any help is much appreciated, thanks. Steve ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Steven Worthington Ph.D. Candidate New York Consortium in Evolutionary Primatology & Department of Anthropology New York University 25 Waverly Place New York, NY 10003 U.S.A. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- View this message in context: http://www.nabble.com/Returning-variable-names-with-variables-%28in-a-fu nction%29-tp17124874p17124874.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. This e-mail may contain confidential and/or privileged i...{{dropped:10}}
Ian Fiske
2008-May-08 12:53 UTC
[R] Re turning variable names with variables (in a function)
You want to set the "names" attribute of your results vector. You can do this with the names() function (see ?names). Specifically, you might use something like this: results <- c(se, upper, lower, cv) names(results) <- c("se", "upper", "lower", "cv") Good luck, Ian Stropharia wrote:> > Dear R Users, > > I have written a function that returns 4 variables. I would like to have > the variables returned with their variable names, is this possible? > > ----------------------------- R Code ------------------------- > mc.error <- function(T, p=0.05){ > se <- sqrt((p)*(1-(p))/T) # standard error > upper <- p+(1.96*se) # upper CI > lower <- p-(1.96*se) # lower CI > cv <- se/p # coefficient of variation > results <- c(se, upper, lower, cv) > return(results) > } > ----------------------------- R Code ------------------------- > > This returns (or something like this, depending on the value of T): > > [1] 0.004998685 0.059797422 0.040202578 0.099973695 > > I would like: > > [1] se=0.004998685 upper=0.059797422 lower=0.040202578 cv=0.099973695 > > Or even better: > > se upper lower cv > 0.004998685 0.059797422 0.040202578 0.099973695 > > Any help is much appreciated, thanks. > > Steve > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Steven Worthington > Ph.D. Candidate > New York Consortium in > Evolutionary Primatology & > Department of Anthropology > New York University > 25 Waverly Place > New York, NY 10003 > U.S.A. > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >-- View this message in context: http://www.nabble.com/Returning-variable-names-with-variables-%28in-a-function%29-tp17124874p17125080.html Sent from the R help mailing list archive at Nabble.com.