[R] How to change several elements of an array simultaneously?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Jan 17 12:20:10 CET 2000


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list