[R] Vectors out of lists?

William Dunlap wdunlap at tibco.com
Wed Nov 17 18:59:03 CET 2010



Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of William Dunlap
> Sent: Wednesday, November 17, 2010 9:42 AM
> To: Eduardo de Oliveira Horta
> Cc: r-help at r-project.org
> Subject: Re: [R] Vectors out of lists?
> 
> 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)
>   }

Here is a comparison of using that Ybar in integrate()
with one that involved a call to Vectorize (which loops
over the elemenents of the input vector).  The results
are identical but the one without Vectorize is much
faster.

  > Y<-list()
  > Y[[1]]<-function(u) sqrt(u)
  > Y[[2]]<-function(u) sin(u)
  > Y[[3]]<-function(u) 1/2*u
  > 
  > Ybar0 <- function(u)mean(sapply(Y,function(fun)fun(u)))
  > integrate(Vectorize(Ybar0),0,1)
  0.4587882 with absolute error < 5.6e-05
  > Ybar1 <-  function(u)  {
  +           result <- Y[[1]](u)
  +           for(Yi in Y[-1]) result <- result + Yi(u)
  +           result/length(Y)
  + }
  > integrate(Ybar1,0,1)
  0.4587882 with absolute error < 5.6e-05
  > system.time(for(i in 1:1000)integrate(Vectorize(Ybar0), 0, 1))
     user  system elapsed 
    18.39    0.00   18.43 
  > system.time(for(i in 1:1000)integrate(Ybar1, 0, 1))
     user  system elapsed 
     0.28    0.00    0.29 

If some of your functions in Y were not vectorized then
you would have to vectorize them.

> 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.oliveirahorta at gmail.com] 
> 	Sent: Tuesday, November 16, 2010 4:15 PM
> 	To: David Winsemius
> 	Cc: Phil Spector; r-help at r-project.org; wwwhsd at 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 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.
> 



More information about the R-help mailing list