[R] Array Dimension Names

Tony Plate tplate at blackmesacapital.com
Fri Oct 31 22:06:45 CET 2003


You can already name dimensions using standard arrays (but you can't use 
these names for the MARGIN argument of apply) e.g.:

 > x <- array(1:6, 3:2, dimnames=list(rows=letters[1:3],cols=LETTERS[24:25]))
 > x
     cols
rows X Y
    a 1 4
    b 2 5
    c 3 6
 > apply(x, 2, sum)
  X  Y
  6 15
 > apply(x, "cols", sum)
Error in -MARGIN : Invalid argument to unary operator
 >

You could pretty easily create your own version of apply() that checked if 
MARGIN was character, and if it were, matched it against names(dimnames(X))

hope this helps,

Tony Plate

At Friday 12:50 PM 10/31/2003 -0800, 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




More information about the R-help mailing list