From deep@y@n@@@rk@r @ending from gm@il@com  Tue Jul 31 08:45:19 2018
From: deep@y@n@@@rk@r @ending from gm@il@com (Deepayan Sarkar)
Date: Tue, 31 Jul 2018 12:15:19 +0530
Subject: [Rd] apply with zero-row matrix
In-Reply-To: <23391.1740.722692.223110@stat.math.ethz.ch>
References: <CAARY7khVXzxutThhKcq+ZftTE8-66eH-B-82xs-8TFyTTmGrkw@mail.gmail.com>
 <CAARY7kjL=buR9GE9rP8Y64D_WTdrFunfTMUXD2mw8P4gMsNrTQ@mail.gmail.com>
 <23390.49459.112386.395212@stat.math.ethz.ch>
 <CAARY7kgmMCbMtigw4i6NZMVMfv-VFdg0VQY1qKECPyAaJ=kbYA@mail.gmail.com>
 <23391.1740.722692.223110@stat.math.ethz.ch>
Message-ID: <CADfFDC73=N7W3Cuw5OQByhHrkpN0UuRezkyuYDrDj3ZFqApaxA@mail.gmail.com>

On Mon, Jul 30, 2018 at 6:08 PM, Martin Maechler
<maechler at stat.math.ethz.ch> wrote:
>>>>>> David Hugh-Jones
>>>>>>     on Mon, 30 Jul 2018 10:12:24 +0100 writes:
>
>     > Hi Martin, Fair enough for R functions in general. But the
>     > behaviour of apply violates the expectation that apply(m,
>     > 1, fun) calls fun n times when m has n rows.  That seems
>     > pretty basic.
>
> Well, that expectation is obviously wrong ;-)  see below
>
>     > Also, I understand from your argument why it makes sense
>     > to call apply and return a special result (presumably
>     > NULL) for an empty argument; but why should apply call fun?
>
>     > Cheers David
>
> The reason is seen e.g. in
>
>     > apply(matrix(,0,3), 2, quantile)
>          [,1] [,2] [,3]
>     0%     NA   NA   NA
>     25%    NA   NA   NA
>     50%    NA   NA   NA
>     75%    NA   NA   NA
>     100%   NA   NA   NA
>     >

I don't think this example is relevant to what David is saying:
matrix(,0,3) has three columns, so he would expect quantile() to be
called 3 times, as it is.

I think his question is why quantile() is called at all when the input
has 0 rows, as in

apply(matrix(,0,3), 1, quantile)
# named numeric(0)

> and that is documented (+/-) in the first paragraph of the
> 'Value:' section of help(apply) :
>
>  > Value:
>  >
>  >      If each call to ?FUN? returns a vector of length ?n?, then ?apply?
>  >      returns an array of dimension ?c(n, dim(X)[MARGIN])? if ?n > 1?.
>  >      If ?n? equals ?1?, ?apply? returns a vector if ?MARGIN? has length
>  >      1 and an array of dimension ?dim(X)[MARGIN]? otherwise.  If ?n? is
>  >      ?0?, the result has length 0 but not necessarily the ?correct?
>  >      dimension.
>
>
> To determine 'n', the function *is* called once even when
> length(X) ==  0

This part of the docs also doesn't seem applicable, and in fact seems
incorrect: here we should have (according to the docs)

n = length(quantile(logical(0))) # 5

but the result does not have dim == c(5, 0) as the docs suggest:

dim(apply(matrix(,0,3), 1, quantile))
# NULL

So the length of the result of calling FUN() seems to be ignored in
this case, and as Emil points out, is only used to determine the mode
of the result.

I can't immediately think of an example where returning NULL instead
would make a difference, but there may well be some.

-Deepayan

> It may indeed be would helpful to add this explicitly to the
> help page  ( <R>/src/library/base/man/apply.Rd ).
> Can you propose a wording (in *.Rd if possible) ?
>
> With regards,
> Martin
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] apply with zero-row matrix
Deepayan Sarkar