vapply has a mandatory FUN.VALUE argument which specifies the type and size
of FUN's return value. This helps when you want to cover the 0-length case
without 'if' statements. You can change your apply calls to vapply calls,
but they will be a bit more complicated. E.g., change
apply(X=myMat
On Mon, Jul 30, 2018 at 6:08 PM, Martin Maechler
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 t
Interesting discussion. I'm not wholly convinced by Martin's and Emil's
arguments. The behaviour seems to violate an obvious expectation (fun is
called once per row) to satisfy a subtle one (result has a guaranteed
dimension and type).
In any case, here's a suggested chunk of rd to go at the end o
Try pmap and related functions in purrr:
pmap(as.data.frame(m), ~ { cat("Called...\n"); print(c(...)) })
## list()
On Mon, Jul 30, 2018 at 12:33 AM, David Hugh-Jones
wrote:
> Forgive me if this has been asked many times before, but I couldn't find
> anything on the mailing lists.
>
> I'd exp
Hi David,
Besides Martins point, there is also the issue that for a lot of cases you
would still like to have the right class returned.
Right now these are returns:
> apply(matrix(NA_integer_,0,5), 1, class)
character(0)
> apply(matrix(NA_integer_,0,5), 1, identit
> 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, th
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.
Also, I understand from your argument why it makes sense to call apply and
return a special result (presumably
> David Hugh-Jones
> on Mon, 30 Jul 2018 05:33:19 +0100 writes:
> Forgive me if this has been asked many times before, but I
> couldn't find anything on the mailing lists.
> I'd expect apply(m, 1, foo) not to call `foo` if m is a
> matrix with zero rows. In fact:
Forgive me if this has been asked many times before, but I couldn't find
anything on the mailing lists.
I'd expect apply(m, 1, foo) not to call `foo` if m is a matrix with zero
rows.
In fact:
m <- matrix(NA, 0, 5)
apply(m, 1, function (x) {cat("Called...\n"); print(x)})
## Called...
## [1] FALSE