Hello R users I was trying to find a less annoying way of naming vectors than: x<-1:10 names(x)[1:length(x)]<-"A" So I tried: x<-1:10 names(x)<-"A" #but this gave only the first element named (as described in the help files) and x<-1:10 names(x)[]<-"A" #but this gave all elements named NA The curious thing with this last option is that if the same line is ran a second time, now the vector gets the name "A" for all elements, which is what is desired names(x)[]<-"A" I'm guessing the first time the names attribute is created and the second time values are given to this attribute. But shouldn't we expect the elements to be all named on the first try with the given value? Cheers Francois [[alternative HTML version deleted]]
Try this: names(x) <- rep("A", length(x)) On Mon, Feb 21, 2011 at 11:44 AM, Francois Rousseu < francoisrousseu@hotmail.com> wrote:> > Hello R users > > I was trying to find a less annoying way of naming vectors than: > > x<-1:10 > names(x)[1:length(x)]<-"A" > > So I tried: > > x<-1:10 > names(x)<-"A" #but this gave only the first element named (as described > in the help files) > > and > > x<-1:10 > names(x)[]<-"A" #but this gave all elements named NA > > The curious thing with this last option is that if the same line is ran a > second time, now the vector gets the name "A" for all elements, which is > what is desired > > names(x)[]<-"A" > > I'm guessing the first time the names attribute is created and the second > time values are given to this attribute. But shouldn't we expect the > elements to be all named on the first try with the given value? > > Cheers > Francois > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
It is indeed an interesting behavior and I have no idea what you could do except what you did, though I would use: names(x) <- rep("A", length(x)) But I don't really understand why you want to give the same name to all elements? There might be another way around depending on your goal Ivan Le 2/21/2011 15:44, Francois Rousseu a ?crit :> Hello R users > > I was trying to find a less annoying way of naming vectors than: > > x<-1:10 > names(x)[1:length(x)]<-"A" > > So I tried: > > x<-1:10 > names(x)<-"A" #but this gave only the first element named (as described in the help files) > > and > > x<-1:10 > names(x)[]<-"A" #but this gave all elements named NA > > The curious thing with this last option is that if the same line is ran a second time, now the vector gets the name "A" for all elements, which is what is desired > > names(x)[]<-"A" > > I'm guessing the first time the names attribute is created and the second time values are given to this attribute. But shouldn't we expect the elements to be all named on the first try with the given value? > > Cheers > Francois > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php