[R] matrix propagation problem

Gregory Carey gregory.carey at colorado.edu
Mon Dec 30 04:21:12 CET 2013


Please see the code and R output below. I cannot understand why a matrix object is being propagated in row major order by default and then in column major order when byrow=TRUE is specified when I use one set of dimensions. But when I use different dimensions, the propagation is correct.
Greg

> pvec <- colMeans(rbind(hapn, hapn2))
> class(pvec)
[1] "numeric"
> length(pvec)
[1] 100
> pvec[1:5]
[1] 0.08263811 0.59034639 0.21173734 0.77819000 0.11360578
> nLoci
[1] 100
> nObs
[1] 500
> pvecMat <- matrix(pvec, nLoci, nObs)
> pvecMat[1:5, 1:7]
           [,1]       [,2]       [,3]       [,4]       [,5]       [,6]       [,7]
[1,] 0.08263811 0.08263811 0.08263811 0.08263811 0.08263811 0.08263811 0.08263811
[2,] 0.59034639 0.59034639 0.59034639 0.59034639 0.59034639 0.59034639 0.59034639
[3,] 0.21173734 0.21173734 0.21173734 0.21173734 0.21173734 0.21173734 0.21173734
[4,] 0.77819000 0.77819000 0.77819000 0.77819000 0.77819000 0.77819000 0.77819000
[5,] 0.11360578 0.11360578 0.11360578 0.11360578 0.11360578 0.11360578 0.11360578
> pvecMat2 <- matrix(pvec, nLoci, nObs, byrow=TRUE)
> pvecMat2[1:5, 1:7]
           [,1]      [,2]      [,3]    [,4]      [,5]     [,6]     [,7]
[1,] 0.08263811 0.5903464 0.2117373 0.77819 0.1136058 0.149802 0.829995
[2,] 0.08263811 0.5903464 0.2117373 0.77819 0.1136058 0.149802 0.829995
[3,] 0.08263811 0.5903464 0.2117373 0.77819 0.1136058 0.149802 0.829995
[4,] 0.08263811 0.5903464 0.2117373 0.77819 0.1136058 0.149802 0.829995
[5,] 0.08263811 0.5903464 0.2117373 0.77819 0.1136058 0.149802 0.829995
> matrix(pvec, 2, 5)
           [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 0.08263811 0.2117373 0.1136058 0.8299950 0.7709431
[2,] 0.59034639 0.7781900 0.1498020 0.6973011 0.3875476


More information about the R-help mailing list