Is R behaving correctly in this example? I do not understand why column 2 has any 2s in it (and why column 3 has any 1s)> x<-matrix(0,10,3) > x[seq(1,10,by=2),2:3]<-c(1,2) > x[,1] [,2] [,3] [1,] 0 1 2 [2,] 0 0 0 [3,] 0 2 1 [4,] 0 0 0 [5,] 0 1 2 [6,] 0 0 0 [7,] 0 2 1 [8,] 0 0 0 [9,] 0 1 2 [10,] 0 0 0 Thank you for any and all help. (I am using 1.5.0 on Windows NT) Niels Waller -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 19 Jun 2002, Niels Waller wrote:> > > Is R behaving correctly in this example? I do not understand why column 2 > has any 2s in it (and why column 3 has any 1s)Why do you think it should not have? Remember the re-cycling rule. You've filled elements *in row-major-order* alternately as 1 and 2.> > x<-matrix(0,10,3) > > x[seq(1,10,by=2),2:3]<-c(1,2) > > x > [,1] [,2] [,3] > [1,] 0 1 2 > [2,] 0 0 0 > [3,] 0 2 1 > [4,] 0 0 0 > [5,] 0 1 2 > [6,] 0 0 0 > [7,] 0 2 1 > [8,] 0 0 0 > [9,] 0 1 2 > [10,] 0 0 0-- Brian D. Ripley, ripley at 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-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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Niels Waller" <niels.waller at vanderbilt.edu> writes:> Is R behaving correctly in this example? I do not understand why column 2 > has any 2s in it (and why column 3 has any 1s) > > > > x<-matrix(0,10,3) > > x[seq(1,10,by=2),2:3]<-c(1,2) > > x > [,1] [,2] [,3] > [1,] 0 1 2 > [2,] 0 0 0 > [3,] 0 2 1 > [4,] 0 0 0 > [5,] 0 1 2 > [6,] 0 0 0 > [7,] 0 2 1 > [8,] 0 0 0 > [9,] 0 1 2 > [10,] 0 0 0 > > Thank you for any and all help. > > (I am using 1.5.0 on Windows NT)c(1,2) is recycled and the 5x2 submatrix is filled by column. You might try x[seq(1,10,by=2),2:3]<-matrix(c(1,2),5,2,byrow=T) -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Yes, that's correct behavior. You are assigning to an extracted submatrix, and what you're assigning is a vector of length two. This gets "recycled" to the correct length, which is ten, and fills the submatrix *column-by-column* (column-major order). You probably wanted to iterate x[i,2:3] <- c(1,2) over i. You could use "rep" to construct the vector like x[seq(1,10,by=2), 2:3] <- rep(c(1,2),c(5,5)) to get this effect. Reid Huntsinger -----Original Message----- From: Niels Waller [mailto:niels.waller at vanderbilt.edu] Sent: Wednesday, June 19, 2002 4:26 PM To: R-Help Subject: [R] unexpected results Is R behaving correctly in this example? I do not understand why column 2 has any 2s in it (and why column 3 has any 1s)> x<-matrix(0,10,3) > x[seq(1,10,by=2),2:3]<-c(1,2) > x[,1] [,2] [,3] [1,] 0 1 2 [2,] 0 0 0 [3,] 0 2 1 [4,] 0 0 0 [5,] 0 1 2 [6,] 0 0 0 [7,] 0 2 1 [8,] 0 0 0 [9,] 0 1 2 [10,] 0 0 0 Thank you for any and all help. (I am using 1.5.0 on Windows NT) Niels Waller -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it. ============================================================================= -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._