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. 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 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Not only changing mode or length (i.e. structure) of the original object results in converting an .Alias into a real copy, as the following examples show:> x <- 1:9 > y <- .Alias(x[9]) > y[1][1] 9> y[1] <- 10 > x[1] 1 2 3 4 5 6 7 8 9 # not [1] 1 2 3 4 5 6 7 8 10 as expected> x <- 1:9 > y <- .Alias(x[9]) > x[9] <- 10 > y[1] 9 # not [1] 10 as expected Why is that? Are these read-ony pointer? Jens> version_ platform Windows arch x86 os Win32 system x86, Win32 status major 1 minor 1.1 year 2000 month August day 15 language R Mit freundlichen Gr??en -- Dr. Jens Oehlschl?gel Analyse BBDO InterOne Gr?nstr. 15 40212 D?sseldorf Tel.: +49 (0)211 1379-187 Fax.: +49 (0)211 1379-461 http://www.bbdo-interone.de -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._