I wondered if the real problem was bigger than your abstract version. OK. Here is one way to do it
> myfunc <- function(x) { nam <- deparse(substitute(x)) val <- mean(x) cat("mean(", nam, ") =", val, "\n") invisible(val) } > ex <- quote(myfunc(x)) > subst <- function(Command, ...) do.call("substitute", list(Command, list(...))) > dat <- data.frame(matrix(rnorm(25), 5, 5)) > vars <- names(dat)[-1] > vars [1] "X2" "X3" "X4" "X5" > for(i in vars) with(dat, eval(subst(ex, x = as.symbol(i)))) mean( X2 ) = -0.08661249 mean( X3 ) = 0.009840606 mean( X4 ) = -0.21054 mean( X5 ) = 0.07321301 > colMeans(dat[, -1]) X2 X3 X4 X5 -0.086612490 0.009840606 -0.210540008 0.073213012 > Now why do I suspect there is something more as well....hmmm...? Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:[EMAIL PROTECTED] http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rick Bilonick Sent: Thursday, 25 October 2007 4:13 PM To: R Help Subject: Re: [R] Executing a Function in a Loop With a ChangingValue foran Argument On Thu, 2007-10-25 at 15:34 +1000, [EMAIL PROTECTED] wrote: > There are many simple ways to do this, if I understand you correctly. > Here is an example > > > dat <- data.frame(matrix(rnorm(25), 5, 5)) > > names(dat) > [1] "X1" "X2" "X3" "X4" "X5" > > vars <- names(dat)[-1] > > vars > [1] "X2" "X3" "X4" "X5" > > myfunc <- function(x) print(mean(x)) > > for(i in dat[, vars]) myfunc(x = i) > [1] 0.3648022 > [1] -0.1593466 > [1] 0.5874517 > [1] -0.5049586 > > colMeans(dat) ## as a check > X1 X2 X3 X4 X5 > 0.1779146 0.3648022 -0.1593466 0.5874517 -0.5049586 > > > > > Bill Venables > Thanks. The problem with this approach is that the data.frame variable names are lost. (A minor point is that it won't work if only one variable is chosen.) The function I wrote generates labels for a plot and I want the variable name to show up (not "i"). I was hoping not to have to re-write my function. Is there a way to pass the variables that will allow my function to work normally (just as it would if I wrote out the function calls with the names manually inserted)? (I would assume that any function that generates a character variable from the variable name is not going to produce the desired result.) Rick B. ______________________________________________ R-help@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. ______________________________________________ R-help@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.