Warnes, Gregory R
2002-Feb-20 21:15 UTC
[Rd] Feature Request: "matrix[1:10,1:10, block=F] <- 1:10"
Hi Guys, I've again been surprised when something like> m <- matrix("", nrow=3,ncol=3) > index_i <- c(2,1,3) > index_j <- c(1,3,2) > vals <- c("a","b","c") > m[ index_i, index_j ] <- vals > mgives block-wise application of the assignment: [,1] [,2] [,3] [1,] "b" "b" "b" [2,] "a" "a" "a" [3,] "c" "c" "c" when what I wanted was by-element assignment: [,1] [,2] [,3] [1,] "" "" "b" [2,] "a" "" "" [3,] "" "c" "" I always end up writing the code:> for( p in length(index_i) ) > m[ index_i[p], index_j[p] ] <- vals[p]to do what I want. So, I'm thinking about creating a patch to the R source to add an optional parameter, say "block", to the "[<-" call for matrixes and data frames. When "block=T" (the default) the old behaviour is preserved. When "block=F" the behavior I want occures so that> m[ index_i, index_j, block=F ] <- valswould do what I want. Comments? Cheers? Jeers? Silence? -Greg 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David Meyer
2002-Feb-20 21:36 UTC
[Rd] Feature Request: "matrix[1:10,1:10, block=F] <- 1:10"
"Warnes, Gregory R" wrote:> > Hi Guys, > > I've again been surprised when something like > > > m <- matrix("", nrow=3,ncol=3) > > index_i <- c(2,1,3) > > index_j <- c(1,3,2) > > vals <- c("a","b","c") > > m[ index_i, index_j ] <- vals > > m > > gives block-wise application of the assignment: > > [,1] [,2] [,3] > [1,] "b" "b" "b" > [2,] "a" "a" "a" > [3,] "c" "c" "c" >Yes, you need to provide the indices as matrix, like: m[cbind(index_i,index_j)] (BTW: Hou do you manage to get index_i <- c(...) properly working? My R version still treats _ as a replacement for <-)> when what I wanted was by-element assignment: > > [,1] [,2] [,3] > [1,] "" "" "b" > [2,] "a" "" "" > [3,] "" "c" "" > > I always end up writing the code: > > > for( p in length(index_i) ) > > m[ index_i[p], index_j[p] ] <- vals[p] > > to do what I want. > > So, I'm thinking about creating a patch to the R source to add an optional > parameter, say "block", to the "[<-" call for matrixes and data frames. > > When "block=T" (the default) the old behaviour is preserved. When "block=F" > the behavior I want occures so that > > > m[ index_i, index_j, block=F ] <- vals > > would do what I want. > > Comments? Cheers? Jeers? Silence? > > -Greg > > > > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Mag. David Meyer Wiedner Hauptstrasse 8-10 Vienna University of Technology A-1040 Vienna/AUSTRIA Department for Tel.: (+431) 58801/10772 Statistics and Probability Theory mail: david.meyer@ci.tuwien.ac.at -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Warnes, Gregory R
2002-Feb-20 22:04 UTC
[Rd] Feature Request: "matrix[1:10,1:10, block=F] <- 1:10"
Hi All, Typo in my code (as pointed out by David Meyer), "index_i" and "index_j" should be "index.i" and "index.j". -Greg> -----Original Message----- > From: David Meyer [mailto:david.meyer@ci.tuwien.ac.at] > Sent: Wednesday, February 20, 2002 4:37 PM > To: Warnes, Gregory R > Cc: 'R-devel@stat.math.ethz.ch' > Subject: Re: [Rd] Feature Request: "matrix[1:10,1:10, > block=F] <- 1:10" > > > "Warnes, Gregory R" wrote: > > > > Hi Guys, > > > > I've again been surprised when something like > > > > > m <- matrix("", nrow=3,ncol=3) > > > index_i <- c(2,1,3) > > > index_j <- c(1,3,2) > > > vals <- c("a","b","c") > > > m[ index_i, index_j ] <- vals > > > m > > > > gives block-wise application of the assignment: > > > > [,1] [,2] [,3] > > [1,] "b" "b" "b" > > [2,] "a" "a" "a" > > [3,] "c" "c" "c" > > > > Yes, you need to provide the indices as matrix, like: > > m[cbind(index_i,index_j)] > > (BTW: Hou do you manage to get index_i <- c(...) properly > working? My R > version still treats _ as a replacement for <-) > > > when what I wanted was by-element assignment: > > > > [,1] [,2] [,3] > > [1,] "" "" "b" > > [2,] "a" "" "" > > [3,] "" "c" "" > > > > I always end up writing the code: > > > > > for( p in length(index_i) ) > > > m[ index_i[p], index_j[p] ] <- vals[p] > > > > to do what I want. > > > > So, I'm thinking about creating a patch to the R source to > add an optional > > parameter, say "block", to the "[<-" call for matrixes and > data frames. > > > > When "block=T" (the default) the old behaviour is > preserved. When "block=F" > > the behavior I want occures so that > > > > > m[ index_i, index_j, block=F ] <- vals > > > > would do what I want. > > > > Comments? Cheers? Jeers? Silence? > > > > -Greg > > > > > > > > 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 -- Readhttp://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 >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -- Mag. David Meyer Wiedner Hauptstrasse 8-10 Vienna University of Technology A-1040 Vienna/AUSTRIA Department for Tel.: (+431) 58801/10772 Statistics and Probability Theory mail: david.meyer@ci.tuwien.ac.at -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David Brahm
2002-Feb-20 22:20 UTC
[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) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._