[R] naming vectors

Peter Ehlers ehlers at ucalgary.ca
Tue Feb 22 02:38:22 CET 2011


On 2011-02-21 07:17, Francois Rousseu wrote:
>
>
> I guess I will just get over my laziness and use the rep function instead! But my goal was more to point out the weird names attribution behaviour.

Maybe not all that weird. Do str(x) between your names<-
assignments:

  x <- 1:10
  str(x)

  names(x)[] <- 'A'
  str(x)  # note that x now has a 'names' attribute

  names(x)[] <- 'A'
  str(x)  # now the names attribute can be modified

Perhaps not intuitive, but not overly strange.

Peter Ehlers

>
> I'm using named vectors of dates that can be associated to 2 stages (incubation and rearing in nesting birds) to overlay a vector of observed data and a vector of theoritical data to see if the stages match on same dates. A data frame could also be used for this task, but I thought that vectors are a bit simpler and faster to create.
>
> Francois Rousseu
>
>> Date: Mon, 21 Feb 2011 15:58:36 +0100
>> From: ivan.calandra at uni-hamburg.de
>> To: r-help at r-project.org
>> Subject: Re: [R] naming vectors
>>
>> 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
>>
>> ______________________________________________
>> 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.
>   		 	   		
> 	[[alternative HTML version deleted]]
>



More information about the R-help mailing list