[R] assigning dimnames to arrays

David Winsemius dwinsemius at comcast.net
Tue Mar 21 16:21:32 CET 2017


> On Mar 20, 2017, at 4:46 PM, Douglas Ezra Morrison <dmorrison01 at ucla.edu> wrote:
> 
> Dear R-Help readers,
> 
> I am writing to ask about some behavior of base::dimnames() that surprised
> me. If I create an array with one of its dimensions = 1, and then try to
> assign names to that dimension, I get an error unless I name one of the
> other dimensions first. For example:
> 
>> temp1 = array(NA, c(3,2,1))
>> dimnames(temp1)[[3]] = "test"
> 
> results in the error: "Error in dimnames(temp1)[[3]] = "test" : 'dimnames'
> must be a list"
> 
> However, the following works:
> 
>> temp1 = array(NA, c(3,2,1))

Why not:

temp1 = array(NA, c(3,2,1))
dimnames(temp1) <- list(NULL,NULL,"test")


>> dimnames(temp1)[[2]] = c("a","b")
>> dimnames(temp1)[[3]] = "test"
> 
> I found an explanation of what is happening on stackoverflow (
> http://stackoverflow.com/questions/12578461/r-dimnames-of-matrix-strange-behaviour/42915723),
> however, I didn't see any explanation of why this behavior is
> intended/desirable. Moreover, it recently caused a problem in a place where
> I couldn't easily work around it, without submitting edits to or forking
> someone else's R package. Is there any possibility that this behavior is a
> bug that could be fixed, or is it something I should learn to live with?

I doubt that a request for changing the behavior of the `dimnames.<-` function will get very far. Learn to use the language as it's designed.

David.

> 
> Thanks,
> Doug
> 
> -- 
> 
> Typed from a mobile device - apologies for typos and brevity.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list