[R] Array Dimension Names

Gabor Grothendieck ggrothendieck at myway.com
Sat Nov 1 00:31:52 CET 2003



You could add attributes to your array when creating it
and then retrieve them:

  x <- matrix(1:8,2,4)
  attr(x,"workers") <- 1
  attr(x,"variables") <- 2

  apply(x,attr(x,"variables"),sum)

or perhaps:

  y <- matrix(1:8,2,4)
  attr(y,"margins") <- list(workers = 1, variables = 2)

  apply(y,attr(y,"margins")$variables,sum) 

These are simple enough that you might not need to develop your
own apply but you could pretty them up even more if you did:

  my.apply <- function(x,dim,fn) apply(x,attr(x,dim),fn)

  my.apply(x,"variables",sum)

---

From: <Benjamin.STABLER at odot.state.or.us>
To: <r-help at stat.math.ethz.ch> 
Subject: [R] Array Dimension Names 

 
 
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


_______________________________________________
No banners. No pop-ups. No kidding.
Introducing My Way - http://www.myway.com




More information about the R-help mailing list