[R] Re: R vrs S

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Oct 14 15:42:39 CEST 1998


"Heberto Ghezzo" <HEBERTO at MEAKINS.Lan.McGill.CA> writes:

> Hi, I am relatively new to R programming. I have Guido's version on a 
> Win95 Pentium with 32 Mb RAM.
> I tried to compile the Package MIXREG of Rolf Turner in S to R
> almost everything works OK but he has a function po = partial outer 
> product as follows:
> po <- function(a,b) {
> array(apply(b,2,"*",a),dim=c(dim(a),dim(b)[2])))   }
> in S-Plus given
> x = ( 1 1
>       1 2
>       1 3
>       1 4 )
> po(x,x) gives
> [,,1] = 1  1
>         1  2
>         1  3
>         1  4
> [,,2] = 1  1
>         2  4
>         3  9
>         4  16
> ie a collection of flats of outer products, now in R the same 
> function gives an error for un conmensurable arrays. I have to re-
> write po as
> po <- function(a,b) {
>   bb <- array(0,dim=c(dim(a),dim(b)[2])                
>   for (i in 1:dim(a)[1]) {
>      bb[i,,] <= outrer(a[i,],b[i,]) }
>   bb
> }
> Am I missing something? . The help files for 'apply' in R and is S+ 
> are exaclty the same, but the implementations obviously are 
> different. . .

Hmmm.... R is a bit stricter than S when it comes to creating the
array slices in apply(). It seems that where S is doing x[,1]*x, R has
array(x[,1],4)*x which is not allowed in either language. 


I'm not sure which language has the bug and which one has the feature,
but a way out is

po <- function(a,b) {
array(apply(b,2,function(x,y)c(x)*y,a),dim=c(dim(a),dim(b)[2]))}
                ^^^^^^^^^^^^^^^^^^^^^^

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