[R] How to create a correct matrix in R

Tiago V. Pereira tiago.pereira at mbe.bio.br
Wed May 22 16:49:59 CEST 2013


Hello Rlisters!

In my codes, I need to import a matrix:

v <- read.table("/home/tiago/matrix.txt", header=FALSE)
v<-as.matrix(v)
v
              V1          V2          V3          V4          V5          V6
[1,]  1.00000000 -0.89847480 -0.73929292 -0.99055335 -0.04514469  0.04056137
[2,] -0.89847480  1.00000000  0.95986852  0.82978466  0.04056137 -0.04514469
[3,] -0.73929292  0.95986852  1.00000000  0.63996937  0.03337515 -0.04333297
[4,] -0.99055335  0.82978466  0.63996937  1.00000000  0.04471823 -0.03746038
[5,] -0.04514469  0.04056137  0.03337515  0.04471823  1.00000000 -0.89847480
[6,]  0.04056137 -0.04514469 -0.04333297 -0.03746038 -0.89847480  1.00000000
[7,] -0.60519045  0.67357531  0.64654374  0.55892246 -0.06244832  0.06950480
              V7
[1,] -0.60519045
[2,]  0.67357531
[3,]  0.64654374
[4,]  0.55892246
[5,] -0.06244832
[6,]  0.06950480
[7,]  1.00000000

However, I keep getting the same error after loading that matrix:

  `v' is not a covariance matrix

Nonetheless, if I input the matrix directly, there is no error:

x1 = c(1, -0.898474804259413, -0.739292919198965, -0.990553354617789,
-0.0451446949071635, 0.0405613709200646, -0.605190448449146)
x2 = c(-0.89847480425931, 1, 0.959868518981255, 0.829784658203916,
0.0405613709200599, -0.0451446949071635, 0.673575314054563)
x3 = c(-0.739292919198939, 0.959868518981239, 1, 0.639969373426519,
0.0333751532842623, -0.0433329714403989, 0.646543739123876)
x4 = c(-0.990553354617685, 0.82978465820392, 0.639969373426531, 1,
0.0447182289834827, -0.0374603752332609, 0.558922461747364)
x5 = c(-0.0451446949071635, 0.0405613709200646, 0.0333751532842635,
0.0447182289834874, 1, -0.898474804259413, -0.0624483157850655)
x6 =
c(0.0405613709200679,	-0.0451446949071635,	-0.0433329714403999,-0.0374603752332612,-0.898474804259486,1,0.0695048046856916)
x7 = c(-0.605190448449077, 0.673575314054563, 0.646543739123887,
0.558922461747361, -0.0624483157850583, 0.0695048046856916, 1)

v <- rbind(x1,x2,x3,x4,x5,x6,x7)
row.names(v)<-NULL
v

            [,1]        [,2]        [,3]        [,4]        [,5]        [,6]
[1,]  1.00000000 -0.89847480 -0.73929292 -0.99055335 -0.04514469  0.04056137
[2,] -0.89847480  1.00000000  0.95986852  0.82978466  0.04056137 -0.04514469
[3,] -0.73929292  0.95986852  1.00000000  0.63996937  0.03337515 -0.04333297
[4,] -0.99055335  0.82978466  0.63996937  1.00000000  0.04471823 -0.03746038
[5,] -0.04514469  0.04056137  0.03337515  0.04471823  1.00000000 -0.89847480
[6,]  0.04056137 -0.04514469 -0.04333297 -0.03746038 -0.89847480  1.00000000
[7,] -0.60519045  0.67357531  0.64654374  0.55892246 -0.06244832  0.06950480
            [,7]
[1,] -0.60519045
[2,]  0.67357531
[3,]  0.64654374
[4,]  0.55892246
[5,] -0.06244832
[6,]  0.06950480
[7,]  1.00000000

How can one import the data correctly?

I could not figure it out.

Thanks in advance.

Tiago



More information about the R-help mailing list