Skip to content
Prev 37287 / 63424 Next

Using 'dimname names' in aperm() and apply()

It's not that simple. These are base functions so

- adding utility functions to base is undesirable
- efficiency matters
- any change (including adding a function!) needs corresponding
   documentation.
- this needs much better error checking.
- dimnum.from.dimnamename is rather inefficient: a simple call to
   match() will do the job.  (And we do have seq_along !)

I've been tidying up aperm(), and there this is as simple to do at C 
level (and more efficient).  For apply(), something like

     if (is.character(MARGIN)) {
         if(is.null(dnn <- names(dn)))
            stop("'X' must have named dimnames")
         MARGIN <- match(MARGIN, dnn)
         if (any(is.na(MARGIN)))
             stop("not all elements of 'perm' are names of dimensions")
     }


Thanks for the suggestions: a version will appear in R-devel in due 
course.
On Thu, 29 Jul 2010, Michael Lachmann wrote:

            
Here for efficiency use rowSums() ....