[R] zero extent arrays and apply()

From: Robin Hankin <r.hankin_at_noc.soton.ac.uk>
Date: Mon 13 Feb 2006 - 23:40:12 EST


Hi

I am having difficulty making apply() work as expected (and desired) with arrays
that have zero-extent dimensions.

?apply says

   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.


My reading of this is that if n=1 and MARGIN has length >1 an array of dimension dim(X)[MARGIN] is returned. But:

> a <- array(0,c(3,0,4))
> dimnames(a) <- list(a=letters[1:3],b=NULL,c=LETTERS[1:4])
> f <- function(x){5}
> apply(a,1:2,f)

numeric(0)
>

I want an array of dimension c(3,0,4)[1:2] here, ie c(3,0). In my application,
the dimnames are the important thing.

Any comments anyone?

--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Feb 13 23:44:46 2006

This archive was generated by hypermail 2.1.8 : Wed 15 Feb 2006 - 13:26:59 EST