[R] .Alias

Setzer.Woodrow@epamail.epa.gov Setzer.Woodrow at epamail.epa.gov
Thu Oct 26 14:54:11 CEST 2000



Thanks.  This actually makes sense, and is consistent with the documentation for
.Alias.  I have tended to think of assignment as it occurs in C or fortran: at
wost, the thing assigned is coerced to match the thing being assigned to.  In R,
it seems, the thing being assigned to gets coerced to match the thing being
assigned.  I can see that this makes sense, but there must be a lot of
unintended consequences to watch out for!


R. Woodrow Setzer, Jr.                                            Phone: (919)
541-0128
Experimental Toxicology Division                       Fax:  (919) 541-5394
Pharmacokinetics Branch
NHEERL MD-55; US EPA; RTP, NC 27711


|--------+------------------------>
|        |          p.dalgaard at bio|
|        |          stat.ku.dk    |
|        |                        |
|        |          10/25/2000    |
|        |          05:44 PM      |
|        |                        |
|--------+------------------------>
  >----------------------------------------------------------------------------|
  |                                                                            |
  |       To:     Woodrow Setzer/RTP/USEPA/US at EPA                              |
  |       cc:     r-help at hypatia.math.ethz.ch                                  |
  |       Subject:     Re: [R] .Alias                                          |
  >----------------------------------------------------------------------------|






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