Warnes, Gregory R
2002-Feb-20 22:44 UTC
[Rd] Bug in "[<-.matrix"? (Was: Feature Request: "matrix[1:10,1:10, block=F] <- 1:10")
Thanks to David Meyer [david.meyer@ci.tuwien.ac.at] and David Brahm [brahm@alum.mit.edu] who suggested: m[ cbind(index.i, index.j) ] <- vals This works fine for the example I gave. Unfortunately, this approach doesn't extend to using the row and column names to make assignments:> m <- matrix("",ncol=3,nrow=3) > dimnames(m) <- list(c("a","b","c"), c("A","B","C")) > mA B C a "" "" "" b "" "" "" c "" "" ""> > index.i <- c("b","a","c") > index.j <- c("B","A","C") > vals <- c("1","2","3") > > m[ cbind(index.i, index.j) ] <- vals > mb a c B A C "" "" "" "" "" "" "" "" "" "1" "2" "3" "1" "2" "3" This looks like a bug... -Greg> -----Original Message----- > From: David Brahm [mailto:brahm@alum.mit.edu] > Sent: Wednesday, February 20, 2002 5:21 PM > To: gregory_r_warnes@groton.pfizer.com > Cc: r-devel@hypatia.math.ethz.ch > Subject: [Rd] Feature Request: "matrix[1:10,1:10, block=F] <- 1:10" > > > Gregory R Warnes <gregory_r_warnes@groton.pfizer.com> wrote: > R> m <- matrix("", nrow=3,ncol=3) > R> index_i <- c(2,1,3) > R> index_j <- c(1,3,2) > R> vals <- c("a","b","c") > R> m[ index_i, index_j ] <- vals > > but what he wants to get is: > > [,1] [,2] [,3] > > [1,] "" "" "b" > > [2,] "a" "" "" > > [3,] "" "c" "" > > Just do this instead: > R> m[ cbind(index_i, index_j) ] <- vals > > -- > -- David Brahm (brahm@alum.mit.edu) >LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel 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-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian Ripley
2002-Feb-21 06:48 UTC
[Rd] Bug in "[<-.matrix"? (Was: Feature Request: "matrix[1:10,1:10, block=F] <- 1:10")
On Wed, 20 Feb 2002, Warnes, Gregory R wrote:> Thanks to David Meyer [david.meyer@ci.tuwien.ac.at] and David Brahm > [brahm@alum.mit.edu] who suggested: > > m[ cbind(index.i, index.j) ] <- vals > > This works fine for the example I gave. > > Unfortunately, this approach doesn't extend to using the row and column > names to make assignments: > > > m <- matrix("",ncol=3,nrow=3) > > dimnames(m) <- list(c("a","b","c"), c("A","B","C")) > > m > A B C > a "" "" "" > b "" "" "" > c "" "" "" > > > > index.i <- c("b","a","c") > > index.j <- c("B","A","C") > > vals <- c("1","2","3") > > > > m[ cbind(index.i, index.j) ] <- vals > > m > b a c B A C > "" "" "" "" "" "" "" "" "" "1" "2" "3" "1" "2" "3" > > This looks like a bug...It's as documented. Take a look at e.g. the description of indexing in Venables & Ripley. You can use match() to turn names into numbers.>From the R Language Definition:It is possible to use a matrix of integers as an index ... S throws an error in your example, and perhaps R should too. But not giving errors when used in undocumented ways is not a bug. -- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel 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-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._