From maechler@stat.math.ethz.ch  Thu Nov 11 16:55:56 1999
From: maechler@stat.math.ethz.ch (maechler@stat.math.ethz.ch)
Date: Thu, 11 Nov 1999 17:55:56 +0100 (MET)
Subject: dimname'less array breaks apply (PR#318)
Message-ID: <199911111655.RAA07580@pubhealth.ku.dk>

>>>>> "MM" == Martin Maechler <maechler@stat.math.ethz.ch> writes:

    MM> Thanks a lot, Chuck!
>>>>> "ChuckB" == Charles C Berry <cberry@tajo.ucsd.edu> writes:

    >>> apply(array(1:20,c(2,2,5)),2:3,function(x) x)
    ChuckB> Error: length of dimnames must match that of dims


    ChuckB> Changing:

    ChuckB> dimnames = if (is.null(dn.ans)) list(ans.names, NULL) else c(list(ans.names), dn.ans)

    ChuckB> To:

    ChuckB> dimnames = if (length(dn)==0) NULL else 
    ChuckB> if (is.null(dn.ans)) list(ans.names, NULL) else c(list(ans.names), dn.ans)

    ChuckB> seems to fix this.

    MM> yes.
    MM> I've committed the patch as follows
    MM> (starting at line 59 in  src/library/base/R/apply.R) :

    MM> if(len.a > 0 && len.a %% d2 == 0)
    MM> return(array(ans, c(len.a %/% d2, d.ans),
    MM> -		     dimnames = if(is.null(dn.ans)) list(ans.names,NULL)
    MM> -		     else c(list(ans.names), dn.ans)))
    MM> +		     dimnames = if(length(dn)) {
    MM> +                         if(is.null(dn.ans)) list(ans.names,NULL)
    MM> +                         else c(list(ans.names), dn.ans)}))
    MM> return(ans)

which is unfortunately wrong (thank, PD!). 
A more correct patch has src/library/base/R/apply.R ending with

    if(len.a > 0 && len.a %% d2 == 0)
	return(array(ans, c(len.a %/% d2, d.ans),
                     if(is.null(dn.ans)) {
                         if(!is.null(ans.names)) list(ans.names,NULL)
                     } else c(list(ans.names), dn.ans)))
    return(ans)
}

MM

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


dimname'less array breaks apply (PR#318)
maechler@stat.math.ethz.ch