Subject: Re: [R] .Alias
From: Peter Dalgaard BSA (p.dalgaard@biostat.ku.dk)
Date: Thu 26 Oct 2000 - 07:44:33 EST
Message-ID: <x2hf60d29q.fsf@blueberry.kubism.ku.dk>
Setzer.Woodrow@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@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@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This archive was generated by hypermail 2b25 : Thu 01 Feb 2001 - 16:14:29 EST