[R] Vectors out of lists?

Joshua Wiley jwiley.psych at gmail.com
Wed Nov 17 19:36:13 CET 2010


On Wed, Nov 17, 2010 at 9:42 AM, William Dunlap <wdunlap at tibco.com> wrote:
> Don't fixate on avoiding loops.   Bury them in a function
> so you don't have to see them and then you just want something
> that does the right thing quickly enough.  (I'm assuming
> this is not a homework/puzzle type problem where you are not
> allowed to use loops).  E.g., the following does the job (with
> no error checking):
>  Ybar <- function(u, Y)  {
>          result <- Y[[1]](u)
>          for(Yi in Y[-1]) result <- result + Yi(u)
>          result/length(Y)
>  }

Since all these functions get passed the same arguments, what about
merging them all together into one function.  It seems like this
process could even be automated from the list, perhaps using body()
somehow?

FOO <- function(u) { ((sqrt(u)) + (sin(u)) + (1/2*u))/3 }
integrate(FOO, 0, 1)

Is there a reason not to do this?

Josh

>
> You could probably shoehorn this into a call to Reduce but there
> must be a for loop in Reduce.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com



More information about the R-help mailing list