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.
It happens because dimnames(originalArray) is NULL and when [[<- extends NULL it turns it into a list if the size of the new element is not one but into a vector with the type of new element if the new element's size is one. > str( `[[<-`(NULL, 3, value="One") ) chr [1:3] NA NA "One" > str( `[[<-`(NULL, 3, value=c("One","Two") )) List of 3 $ : NULL $ : NULL $ : chr [1:2] "One" "Two" > str( `[[<-`(NULL, 3, value=character(0) )) List of 3 $ : NULL $ : NULL $ : chr(0) dimnames(x) <- characterVector could be changed to call as.list() on its right hand side. That would help in this case but would cover up usage errors in other cases. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Mar 21, 2017 at 9:29 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> 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. > > ______________________________________________ > 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.
Thanks, Bill. Sooo... taking the error literally, temp1 = array(NA, c(3,2,1)) dimnames(temp1)[[3]] = list( "test" ) works, even though mode( dimnames(temp1)[[3]] ) yields "character". Setting all the dimension names simultaneously still feels way better. -- Sent from my phone. Please excuse my brevity. On March 21, 2017 9:40:55 AM PDT, William Dunlap <wdunlap at tibco.com> wrote:>It happens because dimnames(originalArray) is NULL and when [[<- >extends >NULL it turns it into a list if the size of the new element is not one >but into a vector with the type of new element if the new element's >size is one. > > str( `[[<-`(NULL, 3, value="One") ) > chr [1:3] NA NA "One" > > str( `[[<-`(NULL, 3, value=c("One","Two") )) > List of 3 > $ : NULL > $ : NULL > $ : chr [1:2] "One" "Two" > > str( `[[<-`(NULL, 3, value=character(0) )) > List of 3 > $ : NULL > $ : NULL > $ : chr(0) >dimnames(x) <- characterVector could be changed to call as.list() on >its right hand side. That would help in this case but would cover up >usage errors in other cases. > > >Bill Dunlap >TIBCO Software >wdunlap tibco.com > > >On Tue, Mar 21, 2017 at 9:29 AM, Jeff Newmiller ><jdnewmil at dcn.davis.ca.us> wrote: >> 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. >> >> ______________________________________________ >> 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.