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
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
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
Oh yeah, thanks. I thought I might write a function such as getNames() that returns the dimension number of the names of the dimnames of an object. That way I don't have to rewrite apply, sweep, and aperm. But even better would be for R to allow character names in addition to index numbers for the MARGIN argument to apply and sweep, and the perm argument to aperm. Thanks again, Ben Stabler>-----Original Message----- >From: Tony Plate [mailto:tplate at blackmesacapital.com] >Sent: Friday, October 31, 2003 1:07 PM >To: STABLER Benjamin; r-help at stat.math.ethz.ch >Subject: Re: [R] Array Dimension Names > > >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 >
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