[Rd] surprising behaviour of names<-
Stavros Macrakis
macrakis at alum.mit.edu
Tue Mar 10 18:35:59 CET 2009
>> (B) you cannot (easily) predict whether or not x will be modified
>> destructively
>
> that's fine, thanks, but i must be terribly stupid as i do not see how
> this explains the examples above. where is the x used by something else
> in the first example, so that 'names<-'(x, 'foo') does *not* modify x
> destructively, while it does in the other cases?
>
> i just can't see how your explanation fits the examples -- it probably
> does, but i beg you show it explicitly.
I think the following shows what Peter was referring to:
In this case, there is only one pointer to the value of x:
x <- c(1,2)
> "names<-"(x,"foo")
foo <NA>
1 2
> x
foo <NA>
1 2
In this case, there are two:
> x <- c(1,2)
> y <- x
> "names<-"(x,"foo")
foo <NA>
1 2
> x
[1] 1 2
> y
[1] 1 2
It seems as though `names<-` and the like cannot be treated as R
functions (which do not modify their arguments) but as special
internal routines which do sometimes modify their arguments.
-s
More information about the R-devel
mailing list