[R] matrix problem

William Simpson william.a.simpson at gmail.com
Mon Apr 21 11:54:03 CEST 2008


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



More information about the R-help mailing list