[R] help

Clark Allan Allan at STATS.uct.ac.za
Mon Sep 5 20:10:51 CEST 2005


howzit dom

i just saw your mail now!

i tried the following and got an error:

> u=v=seq(1,3)
> u
[1] 1 2 3
> v
[1] 1 2 3

> f=function(x,y){max(x+y-1,0)}
> z=outer(u,v,f)
Error in outer(u, v, f) : dim<- : dims [product 9] do not match the
length of object [1]

it seems s if r is not performing elementwise maximisation!!! 

all you need is:

f=function(x,y){pmax(x+y-1,0)}
z=outer(u,v,f)

ok lets try it!!! i used length =5 to save space

u=v=seq(0,1,length=5)

f=function(x,y){pmax(x+y-1,0)}
z=outer(u,v,f)
z
     [,1] [,2] [,3] [,4] [,5]
[1,]    0 0.00 0.00 0.00 0.00
[2,]    0 0.00 0.00 0.00 0.25
[3,]    0 0.00 0.00 0.25 0.50
[4,]    0 0.00 0.25 0.50 0.75
[5,]    0 0.25 0.50 0.75 1.00

think this works!

check ?pmax

see ya
allan

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