[Rd] surprising behaviour of names<-

Tony Plate tplate at acm.org
Sat Mar 14 00:24:35 CET 2009


Wacek Kusnierczyk wrote:
> Tony Plate wrote:
>   
>> Wacek Kusnierczyk wrote:
>>     
>>> [snip]
>>> i just can't get it why the manual does not manifestly explain what
>>> 'names<-' does, and leaves you doing the guesswork you suggest.
>>>
>>>   
>>>       
>> I'm having trouble understanding the point of this discussion. 
>> Someone is calling a replacement function in a way that it's not meant
>> to be used, and is them complaining about it not doing what he thinks
>> it should, or about the documentation not describing what happens when
>> one does that?
>>     
>
> where is it written that the function is not meant to be used this way? 
> you get an example in the man page, showing precisely how it could be
> used that way.  it also explains the value of 'names<-':
>
> "
>  For 'names<-', the updated object.  (Note that the value of
>      'names(x) <- value' is that of the assignment, 'value', not the
>      return value from the left-hand side.)
> "
>
> it does speak of 'names<-' used in prefix form, and does not do it in
> any negative (discouraging) way.
>
>   
>> Is there anything incorrect or missing in the help page for normal
>> usage of the replacement function for 'names'? (i.e., when used in an
>> expression like 'names(x) <- ...')
>>     
>
> what is missing here in the first place is a specification of what
> 'normal' means.  as far as i can see from the man page, 'normal' does
> not exclude prefix use.  and if so, what is missing in the help page is
> a clear statement what an application of 'names<-' will do, in the sense
> of what a user may observe.
>   
Fair enough.  I looked at the help page for "names" after sending my 
email, and was surprised to see the following in the "DETAILS" section:

   "It is possible to update just part of the names attribute via the 
general rules: see the examples. This works because the expression there 
is evaluated as |z <- "names<-"(z, "[<-"(names(z), 3, "c2"))|. "

To me, this paragraph is far more confusing than enlightening, 
especially as also gives the impression that it's OK to use a 
replacement function in a functional form.  In my own personal opinion 
it would be a enhancement to remove that example from the documentation, 
and just say you can do things like 'names(x)[2:3] <- c("a","b")'.

I often use name replacement functions in a functional way, and because 
one can't use 'names<-' etc in this way, I define my own functions like 
the following:

set.names <- function(n,x) {names(x) <- n; x}

(and similarly for set.rownames(), set colnames(), etc.)

I would highly recommend you do this rather than try to use a call like 
"names<-"(x, ...).

-- Tony Plate

(I guess that if on the label of fridge there is a picture of a guy 
carrying it on his back, then Mr. Fridge-Racer might have some grounds 
for suing.)



More information about the R-devel mailing list