Skip to content

Simple question on eval

3 messages · Kelvin Lam, Ivan Calandra, Joshua Wiley

#
Thank you so much Joshua.  That's exactly what I am looking for.

What I wanted to do is to pass a parameter to a function and I have to run
the functions 30 times.  Instead of typing them all out, I created a long
string of "f(a);f(b);f(c) ..." using paste() and use eval and parse to
evaluative them all at once.  I am sure there are better ways of doing it
but I just know this.
#
Isn't it what do.call() does?

Ivan

Le 12/1/2010 16:39, Lamke a ?crit :

  
    
#
Here are two options that might work for you given the little bit you've said:

## If its the same parameter all 30 times
## say, for example, base = 4.5 to log
for(i in 1:30) {
  print(log(1:10, base = 4.5))
}

## if they are different parameters, you could try
lapply(X = c(1.3, 3, 2.2, 4, 5), FUN = function(x) {
  log(1:10, base = x)})

## the 'X' argument to lapply() is based, one at a time,
## to whatever the function is (the 'FUN' argument)
## so the above is equivalent to
log(1:10, base = 1.3)
log(1:10, base = 3)
log(1:10, base = 2.2)
log(1:10, base = 4)
log(1:10, base = 5)
## but easier to type

Cheers,

Josh

@Ivan do.call() passes a list of arguments, but I am not sure it would
easily adapt to passing 30 different arguments one at a time or even
the same argument 30 times.
On Wed, Dec 1, 2010 at 7:39 AM, Lamke <lamkelf at gmail.com> wrote: