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)
  }
 
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 

 



________________________________

        From: Eduardo de Oliveira Horta
[mailto:eduardo.oliveiraho...@gmail.com] 
        Sent: Tuesday, November 16, 2010 4:15 PM
        To: David Winsemius
        Cc: Phil Spector; r-help@r-project.org; www...@gmail.com;
William Dunlap
        Subject: Re: [R] Vectors out of lists?
        
        
        Thanks, guys... but it seems these suggestions won't work.
        
        Let me try to be more specific with a simple example:
        
        Y<-list()
        Y[[1]]<-function(u) sqrt(u)
        Y[[2]]<-function(u) sin(u)
        Y[[3]]<-function(u) 1/2*u
        
        I wanted something equivalent to
        
        Ybar<-function(u){
           1/3*(Y[[1]](u) + Y[[2]](u) + Y[[3]](u))
        }
        
        but with arbitrary length(Y) and without using any loops. Also,
I can't allow for discretization, since I must be able to evaluate Ybar
at any u, as I'm going to integrate it with the function "integrate".
        
        Thanks again,
        
        Eduardo Horta
        


        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to