[R] problems with outer ( was Re: help )

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Mon Sep 5 18:05:00 CEST 2005


[[ Please use a meaningful subject line. Thank you. ]]

Dominique,

I am unable to reproduce your example in R-2.1.1. Can you reproduce this
in a fresh session of R and state which version are you using ?

 u <- v <- seq(0, 1, length=30)
 f <- function(x, y){ cp=max(x+y-1,0) }
 z <- outer(u, v, f)
Error in outer(u, v, f) : dim<- : dims [product 900] do not match the
length ofobject [1]



As shown in the R FAQ 7.17 (http://tinyurl.com/amofj), you will need to
write a wrapper function. This works for me.

 wrapper <- function(x, y, my.fun, ...) {
      sapply(seq(along = x), FUN = function(i) f(x[i], y[i], ...))
  }
 z <- outer(u, v, wrapper)

dim(z)
[1] 30 30

range(z)
[1] 0 1


Regards, Adai

 


On Mon, 2005-09-05 at 14:06 +0200, Dominique Katshunga wrote:
> Dear helpeRs,
> I seem to be a little bit confused on the result I am getting from the
> few codes below:
> > u=v=seq(0,1,length=30)
> > u
>  [1] 0.00000000 0.03448276 0.06896552 0.10344828 0.13793103 0.17241379
>  [7] 0.20689655 0.24137931 0.27586207 0.31034483 0.34482759 0.37931034
> [13] 0.41379310 0.44827586 0.48275862 0.51724138 0.55172414 0.58620690
> [19] 0.62068966 0.65517241 0.68965517 0.72413793 0.75862069 0.79310345
> [25] 0.82758621 0.86206897 0.89655172 0.93103448 0.96551724 1.00000000
> > v
>  [1] 0.00000000 0.03448276 0.06896552 0.10344828 0.13793103 0.17241379
>  [7] 0.20689655 0.24137931 0.27586207 0.31034483 0.34482759 0.37931034
> [13] 0.41379310 0.44827586 0.48275862 0.51724138 0.55172414 0.58620690
> [19] 0.62068966 0.65517241 0.68965517 0.72413793 0.75862069 0.79310345
> [25] 0.82758621 0.86206897 0.89655172 0.93103448 0.96551724 1.00000000
> > f=function(x,y){cp=max(x+y-1,0)}
> > z=outer(u,v,f)
> z is a 30x30 matrix which is fine, but why all its entries are equal to
> 1? for example, the maximum between u[22]+v[22]-1 and 0 is not 1?? I
> don't really know where I went wrong! 
> thanks,
> Dominique
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list