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)) > 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? Thanks, Doug -- Typed from a mobile device - apologies for typos and brevity. [[alternative HTML version deleted]]
> 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
While I generally agree that it is better to design code that sets all of the dimension names simultaneously, the discrepancy between behavior when the dimensions are longer than 1 and when they are equal to 1 seems irregular. Someone went to some lengths to make it possible to set dimnames individually "most" of the time and it is not clear to me why they stopped short of all the time. -- Sent from my phone. Please excuse my brevity. On March 21, 2017 8:21:32 AM PDT, David Winsemius <dwinsemius at comcast.net> wrote:> >> 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 > >______________________________________________ >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.