[Rd] Suggestion: Dimension-sensitive attributes

Bengoechea Bartolomé Enrique (SIES 73) enrique.bengoechea at credit-suisse.com
Thu Jul 9 10:01:12 CEST 2009


I've also had several use cases where I needed "cell-like" attributes, that is, attributes that have the same dimensions as the original array and are subsetted in the same way --along all its dimensions.

So we're talking about a way to add metadata to matrices/arrays at 3 possible levels:

	1) at the "whole object" level: attributes that are not dropped on subsetting 
	2) at the "dimension" level: attributes that behave like "dimnames", i.e. subsetted along each dimension
	3) at the "cell" level: attributes that are subsetted in the same way as the original array

My proposal would be simpler that Tony's suggestion: like "dimnames", just have reserved attribute names for each case, say "objdata", "dimdata", and "celldata" (or "objattr", "dimattr" and "cellattr").

On the other hand, Tony's pattern would allow as many attributes of each type as necessary (some multiplicity is already possible with the simpler design as dimdata or celldata could be lists of lists), at the cost of a more complex scheme of attributes that needs to be "parsed" each time.

On Tony's suggestion, "attr.keep.on.subset" and "attr.dimname.like" (and possible "attr.cell.like") could be kept on a single list with 3 elements, something like:

> attr(x, "attr.subset.with") <- list(object=..., dims=..., cells=...)

Would something like this make sense for R-core --either for standard arrays or as a new class-- or would it be better implemented in a package?

Enrique

-----Original Message-----
From: Tony Plate [mailto:tplate at acm.org] 
Sent: miércoles, 08 de julio de 2009 18:01
To: r-devel at r-project.org
Cc: Bengoechea Bartolomé Enrique (SIES 73); Henrik Bengtsson
Subject: Re: [Rd] Suggestion: Dimension-sensitive attributes

There have been times when I've thought this could be useful too.

One way to go about it could be to introduce a special attribute that controls how attributes are dealt with in subsetting, e.g., "attr.dimname.like".  The contents of this would be character data; on subsetting, any attribute that had a name appearing in this vector would be treated as a dimension.  At the same time, it might be nice to also introduce "attr.keep.on.subset", which would specify which attributes should be kept on the result of a subsetting operation (could be useful for attributes that specify units).  This of course could be a way of implementing Henrik's suggestion: dimattr(x, "misc") <- value would add "misc" to the "attr.dimname.like" attribute and also set the attribute 
"misc".  The tricky part would be modifying the "[" methods.   However, 
the most useful would probably be the one for ordinary matrices and arrays, and others could be modified when and if their maintainers see the need.

-- Tony Plate

Bengoechea Bartolomé Enrique (SIES 73) wrote:
> Hi,
>
> I agree with Henrik that his suggestion to have "dimension vector attributes" working like dimnames (see below) would be an extremely useful infrastructure adittion to R.
>
> If this is not considered for R-core, I am happy to try to implement this in a package, as a new class. And possibly do the same thing for data frames. Should you have any comments, ideas or suggestions about it, please share!
>
> Best,
>
> Enrique
>
> ----------------------------------------------------------------------
> -------
> Subject: 
> From: Henrik Bengtsson <hb_at_stat.berkeley.edu
> Date: Sun, 07 Jun 2009 14:42:08 -0700
>
> Hi,
>
> maybe this has been suggested before, but would it be possible, without not breaking too much existing code, to add other "dimension vector attributes" in addition to 'dimnames'? These attributes would then be subsetted just like dimnames. 
>
> Something like this: 
>
>   
>> x <- array(1:30, dim=c(2,3,5))
>> dimnames(x) <- list(c("a", "b"), c("a1", "a2", "a3"), NULL); 
>> dimattr(x, "misc") <- list(1:2, list(x=1:5, y=letters[1:8], z=NA), 
>> letters[1:5]);
>>     
>
>
>   
>> y <- x[,1:2,2:3]
>> str(dimnames(y))
>>     
>
> List of 3 
>
>  $ : chr [1:2] "a" "b"
>  $ : chr [1:2] "a1" "a2"
>  $ : NULL
>
>
>   
>> str(dimattr(x, "misc")) 
>>     
>
> List of 3 
>  $ : int [1:2] 1 2 
>  $ :List of 2 
>   ..$ x: int [1:5] 1 2 3 4 5 
>   ..$ y: chr [1:8] "a" "b" "c" "d" ... 
>  $ : chr [1:2] "b" "c" 
>
>  I can imagine this needs to be added in several places and functions such as is.vector() needs to be updated etc. It is not a quick migration, but is it something worth considering for the future? 
>
> /Henrik 
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>   



More information about the R-devel mailing list