Re: [Rd] apply with zero-row matrix

2018-08-13 Thread William Dunlap via R-devel
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

Re: [Rd] apply with zero-row matrix

2018-07-31 Thread Deepayan Sarkar
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

Re: [Rd] apply with zero-row matrix

2018-07-30 Thread David Hugh-Jones
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

Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Gabor Grothendieck
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

Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Emil Bode
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,

Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Martin Maechler
> 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,

Re: [Rd] apply with zero-row matrix

2018-07-30 Thread David Hugh-Jones
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

Re: [Rd] apply with zero-row matrix

2018-07-30 Thread Martin Maechler
> 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:

[Rd] apply with zero-row matrix

2018-07-29 Thread David Hugh-Jones
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