[R] .Alias

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Oct 25 23:44:33 CEST 2000


Setzer.Woodrow at epamail.epa.gov writes:

> Probably I've just misread the documentation, but I don't understand the
> behavior of .Alias.
> Consider this (on R-1.1.1, both Windows and SGI):
> 
> > tmp <- matrix(nrow=3,ncol=2)
> > new <- .Alias(tmp)
> > new[1,1] <- 1
> > tmp
>      [,1] [,2]
> [1,]   NA   NA
> [2,]   NA   NA
> [3,]   NA   NA
> > new
>      [,1] [,2]
> [1,]    1   NA
> [2,]   NA   NA
> [3,]   NA   NA
> 
> I expected tmp[1,1] to be 1.
> 

Heh... This stuff is tricky. The only sound advice is that unless you
know precisely what is going on, don't rely on assignment not to
cause object duplication. 

To add to your confusion, had you used matrix(0, nrow=3, ncol=2), then
indeed new and tmp would have stayed identical.

The reason? Take a look at this:

> tmp <- matrix(nrow=3,ncol=2)
> new <- .Alias(tmp)
> new[1,1] <- 1
> mode(new[2,2])
[1] "numeric"
> mode(tmp[2,2])
[1] "logical"

In the element assignment, there's a coercion to integer of the entire
array first, and this breaks the connection between "new" and "tmp".
 
(Yes, there's a reason that NAs are mode logical by default...)

-- 
   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