[R] Array Dimension Names

Ben Bolker bolker at zoo.ufl.edu
Fri Oct 31 22:13:28 CET 2003


  Not that I know of.  BUT dimnames can themselves have names attributes, 
so a very small hack to apply() will do what you want.

I did 

dump("apply",file="apply.R")

and added the following lines after "dn <- dimnames(X)" (line 14) [this is 
in R 1.7.1].

    if (is.character(MARGIN)) {
      if (is.null(dn) stop("dimnames(X) must have names")
      MARGIN <- match(MARGIN,names(dn))
    }

and then did 

source("apply.R")
x = array(1,dim=c(2,2,2))
dimnames(x) = list(a=1:2,b=1:2,c=1:2)
apply(x,"a",sum)
apply(x,c("a","b"),sum)

On Fri, 31 Oct 2003 Benjamin.STABLER at odot.state.or.us wrote:

> I would like to reference array dimensions by name in an apply and a summary
> function.  For example:
> 
> apply(x, "workers", sum)
> 
> Is there a better way to do this than creating a new attribute for the array
> and then creating new methods for apply and summary?  I don't want to name
> the individual elements of each dimension (such as with dimnames) but rather
> name the dimensions.  Thanks for your help.
> 
> Benjamin Stabler
> Transportation Planning Analysis Unit
> Oregon Department of Transportation
> 555 13th Street NE, Suite 2
> Salem, OR 97301  Ph: 503-986-4104
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
620B Bartram Hall                            bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704




More information about the R-help mailing list