[R] .Alias

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Oct 26 17:30:31 CEST 2000

Jens Oehlschlägel <jens.oehlschlaegel at bbdo-interone.de> writes:

> > You can only .Alias entire R objects, and x[9] is not one, it is an
> > expression which can be evaluated to an R object and you're
> > effectively .Aliasing the unnamed result of that evaluation.
> the help file says:
>   `.Alias' creates an alias to another (part of) an R object
>                                       ^^^^^^^
> so may be the help file needs a fix (but see at the end of my mail)
> quite unpredictable, isn't it

Think deeper. You need to be very careful considering which kinds of
extraction will create a new object and which ones will return the
actual part of the original, and also which kinds of assignments that
are replacements and which modify the original. The use of .Alias
sometimes reveals optimizations in the R evaluator which weren't
really intended to be openly visible and which it is hardly useful to
try and rely on.


x <- y$a

It is clear that the object needs to be copied at some stage. A
simpleminded implementation of R might create first a copy to hold the
result of y$a and then copy that again before assigning to x. Some
complicated expression would lead to quite a bit of duplication, which
is no fun if the object involved is a large dataframe. However, R
tries to be smarter, and it can be that in two ways: either not copy
when extracting and just give a pointer to the object inside y or by
not making the copy before assignment - but not both, obviously. R
does much of this by keeping track of whether an object is "named", in
which case it is not safe not to duplicate it before certain
operations such as assignment. In the above case y$a does not involve
a copy but it will be a named object and hence the assignment will
create the copy. In contrast,

x <- y[["a"]]

will have the [[ operator create an unnamed copy and that can be
assigned directly to x. This is not quite consistent with the $
operation, but keep in mind that [[ can get used in other contexts
where $ can not.

Alias just removes "namedness" from an object so that assignment will
be non-copying, but that only works if copying has not already taken
place when its argument is evaluated.

Notice that the individual elements of a numeric vector are not
themselves R objects. In x <- c(7,9,13) what you really have in memory
is one node and a data block containing three numeric values.
Accessing x[3] must create a new node with a block of 1 value and fill
that data with 13. Hence the result of index operations are always

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list