Peter Holzer
2000-Jan-17 11:01 UTC
[R] How to change several elements of an array simultaneously?
I have a matrix `m' and two vectors `rs' and `cs'. I want to change m at the positions (rs[1], cs[1]), (rs[2], cs[2]), ... to 1. Obviously I can do that by> m[rs + (cs-1) * dim(m)[1]] <- 1but I would just be interested whether I am missing a more "intuitive" way of doing that. Thanks in advance, Peter ____________________________________________________________ Peter Holzer phone: + 41 1 632 46 34 Seminar fuer Statistik, LEO C14 fax: + 41 1 632 12 28 (Leonhardstr. 27) <holzer at stat.math.ethz.ch> ETH (Federal Inst. Technology) 8092 Zurich http://www.stat.math.ethz.ch/~holzer/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2000-Jan-17 11:20 UTC
[R] How to change several elements of an array simultaneously?
Peter Holzer <holzer at stat.math.ethz.ch> writes:> I have a matrix `m' and two vectors `rs' and `cs'. I want to change m at the > positions (rs[1], cs[1]), (rs[2], cs[2]), ... to 1. Obviously I can do that by > > > m[rs + (cs-1) * dim(m)[1]] <- 1 > > but I would just be interested whether I am missing a more "intuitive" way > of doing that.This one is probably a candidate for the title of "most often overlooked feature": m[cbind(rs,cs)] <- 1 -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._