Hi Everyone, I am running into a problem with matrices. I use R version 2.4.1 and an older version. The problem is this: m<-matrix(ncol=3,nrow=4) m[,1:3]<-runif(n=4) That does what I expect; it fills up the rows of the matrix with the data vector> m[,1] [,2] [,3] [1,] 0.2083071 0.2083071 0.2083071 [2,] 0.5865763 0.5865763 0.5865763 [3,] 0.7901782 0.7901782 0.7901782 [4,] 0.8298317 0.8298317 0.8298317 But this doesn't work: m[1:4,]<-runif(n=3)> m[,1] [,2] [,3] [1,] 0.96864939 0.11656740 0.06182311 [2,] 0.11656740 0.06182311 0.96864939 [3,] 0.06182311 0.96864939 0.11656740 [4,] 0.96864939 0.11656740 0.06182311 I want it to fill up the columns of the matrix with the data vector. Maybe there is a better way to do what I want. I need to do both of the above. The matrices are large, so I need a fast method. Thanks very much for any help. Bill Simpson
On 4/21/2008 5:54 AM, William Simpson wrote:> Hi Everyone, > > I am running into a problem with matrices. I use R version 2.4.1 and > an older version. > > The problem is this: > m<-matrix(ncol=3,nrow=4) > m[,1:3]<-runif(n=4) > > That does what I expect; it fills up the rows of the matrix with the > data vector >> m > [,1] [,2] [,3] > [1,] 0.2083071 0.2083071 0.2083071 > [2,] 0.5865763 0.5865763 0.5865763 > [3,] 0.7901782 0.7901782 0.7901782 > [4,] 0.8298317 0.8298317 0.8298317 > > But this doesn't work: > m[1:4,]<-runif(n=3) >> m > [,1] [,2] [,3] > [1,] 0.96864939 0.11656740 0.06182311 > [2,] 0.11656740 0.06182311 0.96864939 > [3,] 0.06182311 0.96864939 0.11656740 > [4,] 0.96864939 0.11656740 0.06182311 > > I want it to fill up the columns of the matrix with the data vector.Does this help? > matrix(runif(4), ncol=3, nrow=4) [,1] [,2] [,3] [1,] 0.60226296 0.60226296 0.60226296 [2,] 0.74104084 0.74104084 0.74104084 [3,] 0.70955138 0.70955138 0.70955138 [4,] 0.03136881 0.03136881 0.03136881 > matrix(runif(3), ncol=3, nrow=4, byrow=TRUE) [,1] [,2] [,3] [1,] 0.7008625 0.8348078 0.1003123 [2,] 0.7008625 0.8348078 0.1003123 [3,] 0.7008625 0.8348078 0.1003123 [4,] 0.7008625 0.8348078 0.1003123> Maybe there is a better way to do what I want. I need to do both of > the above. The matrices are large, so I need a fast method. > > Thanks very much for any help. > > Bill Simpson > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Hi Not sure what you want to do. You can set dimensions to your vector. vec<-1:12 dim(vec)<-c(3,4) you can repeat your vector for n times vec<-rep(1:4,3) dim(vec) <- c(4,3) or you can use byrow option vec<-1:12 matrix(vec, nrow=4, ncol=3, byrow=T) Petr Pikal petr.pikal at precheza.cz 724008364, 581252140, 581252257 r-help-bounces at r-project.org napsal dne 21.04.2008 11:54:03:> Hi Everyone, > > I am running into a problem with matrices. I use R version 2.4.1 and > an older version. > > The problem is this: > m<-matrix(ncol=3,nrow=4) > m[,1:3]<-runif(n=4) > > That does what I expect; it fills up the rows of the matrix with the > data vector > > m > [,1] [,2] [,3] > [1,] 0.2083071 0.2083071 0.2083071 > [2,] 0.5865763 0.5865763 0.5865763 > [3,] 0.7901782 0.7901782 0.7901782 > [4,] 0.8298317 0.8298317 0.8298317 > > But this doesn't work: > m[1:4,]<-runif(n=3) > > m > [,1] [,2] [,3] > [1,] 0.96864939 0.11656740 0.06182311 > [2,] 0.11656740 0.06182311 0.96864939 > [3,] 0.06182311 0.96864939 0.11656740 > [4,] 0.96864939 0.11656740 0.06182311 > > I want it to fill up the columns of the matrix with the data vector. > > Maybe there is a better way to do what I want. I need to do both of > the above. The matrices are large, so I need a fast method. > > Thanks very much for any help. > > Bill Simpson > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On 21/04/2008, at 9:54 PM, William Simpson wrote:> Hi Everyone, > > I am running into a problem with matrices. I use R version 2.4.1 and > an older version. > > The problem is this: > m<-matrix(ncol=3,nrow=4) > m[,1:3]<-runif(n=4) > > That does what I expect; it fills up the rows of the matrix with the > data vector >> m > [,1] [,2] [,3] > [1,] 0.2083071 0.2083071 0.2083071 > [2,] 0.5865763 0.5865763 0.5865763 > [3,] 0.7901782 0.7901782 0.7901782 > [4,] 0.8298317 0.8298317 0.8298317 > > But this doesn't work: > m[1:4,]<-runif(n=3) >> m > [,1] [,2] [,3] > [1,] 0.96864939 0.11656740 0.06182311 > [2,] 0.11656740 0.06182311 0.96864939 > [3,] 0.06182311 0.96864939 0.11656740 > [4,] 0.96864939 0.11656740 0.06182311 > > I want it to fill up the columns of the matrix with the data vector. > > Maybe there is a better way to do what I want. I need to do both of > the above. The matrices are large, so I need a fast method.R fills arrays in ``reverse odometer order'' --- the first index (row index in matrices) ticks over fastest; the second index second fastest. Unless you tell it to do otherwise. In other words matrices get filled up column by column. Unless other instructions are given. Thus m[] <- runif(3) (note that the ``1:4'' is redundant) puts the generated 3-vector into the first 3 entries of column 1, then starts over again, putting the first entry of that vector into the last entry of column 1, then the next two entries of the vector into the first two entries of column 2, and so on. What you want to do is m <- matrix(runif(3),nrow=4,ncol=3,byrow=TRUE) i.e. it is unnecessary and counter-productive to create m beforehand and then try to fill it up. cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}