[R] problems with outer

Faheem Mitha faheem at email.unc.edu
Sun May 7 04:57:36 CEST 2000


(replying to my own message)

Ok, I took a look around, and what I think vectorised means is that if x
and y are two vectors, then the shorter of the vectors is replicated to be
the length of the longer of the two vectors, ie. the elements of the
shorter vector are repeated as necessary (thus the two vectors become the
same length) and then a vector of function values is returned that is the
value of the function elementwise on these two vectors. (Thanks to S
Burn's `S Poetry' for a clear explanation of this on pg 4). I am still not
clear how I would vectorise my function though, assuming that it only
accepts scalar values.

Cam some kind person in any case confirm that this is what was meant?

f.vect <- function(x,y) sapply(seq(along=x),function(i)f(x[i],y[i]))

seems to be to only work correctly if x and y are the same length.

                        Sincerely, Faheem Mitha.

On Sat, 6 May 2000, Faheem Mitha wrote:

> 
> 
> On 7 May 2000, Peter Dalgaard BSA wrote:
> 
> > Faheem Mitha <faheem at email.unc.edu> writes:
> > 
> > > Note that my function tempexpbinsumsq merely exists for the purpose of
> > > outer. I could have done 
> > > tempmatrix <-
> > > outer(x,y,function(x,y) expbinsumsq(point,pair,x,y,a,b,theta))
> > > but I don't know if that would be correct usage.
> > > 
> > > Can someone explain what is going on? I'm at my wits end.
> > 
> > The thing that usually tricks beginners with outer() is that the
> > function has to be vectorised. I.e. if you stick in vectors for x and
> > y, you get a vector result back. Otherwise, you have to vectorise it
> > yourself, e.g. if f takes scalar arguments,
> > 
> > f.vect <- function(x,y) sapply(seq(along=x),function(i)f(x[i],y[i]))
> 
> This is helpful. However I'd like to clarify the meaning of vectorisation
> in this case.
> 
> Does a vectorised function with two arguments mean that if X, Y vectors
> then f(X,Y)= (f(X_i,Y_i)), ie f(X,Y) is the vector with component
> f(X_i,Y_i)?
> 
> This is what appears to be the case from the line beginning f.vect above.
> In particular, this would force x and y to be the same length. If this is
> not the case then I am puzzled how f.vect would be what I want.
> 
> A priori, I could have taken this to mean that f must satisfy the
> following:
>  
> If X is a vector, and y is a scalar, then f(X,y) = (f(X_i,y)) ie f(X,y) is
> the vector with components (f(X_i,y) and similarly for f(x,Y). But this is
> not what you mean, is it?
> 
> Please excuse any confusion. Trying to debug stuff always wears me out, so
> I am not at my best right now.
> 
> Thanks.                          Faheem.
> 
>  -- 
> >    O__  ---- Peter Dalgaard             Blegdamsvej 3  
> >   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
> >  (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
> > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
> > 
> 
> 
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list