[R] How to create a correct matrix in R
arun
smartpink111 at yahoo.com
Wed May 22 19:58:18 CEST 2013
Hi,
Couldn't reproduce your error.
It is better to dput() the example data:
v <- read.table("/home/tiago/matrix.txt", header=FALSE)
dput(v)
v<- read.table("matrix1.txt",header=FALSE,sep="")
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
str(v)
# num [1:7, 1:7] 1 -0.8985 -0.7393 -0.9906 -0.0451 ...
#- attr(*, "dimnames")=List of 2
#..$ : NULL
#..$ : chr [1:7] "V1" "V2" "V3" "V4" ...
#or
v2<- read.table(text="
1.00000000 -0.89847480 -0.73929292 -0.99055335 -0.04514469 0.04056137 -0.60519045
-0.89847480 1.00000000 0.95986852 0.82978466 0.04056137 -0.04514469 0.67357531
-0.73929292 0.95986852 1.00000000 0.63996937 0.03337515 -0.04333297 0.64654374
-0.99055335 0.82978466 0.63996937 1.00000000 0.04471823 -0.03746038 0.55892246
-0.04514469 0.04056137 0.03337515 0.04471823 1.00000000 -0.89847480 -0.06244832
0.04056137 -0.04514469 -0.04333297 -0.03746038 -0.89847480 1.00000000 0.06950480
-0.60519045 0.67357531 0.64654374 0.55892246 -0.06244832 0.06950480 1.00000000
",sep="",header=FALSE)
v2<- as.matrix(v2)
identical(v,v2)
#[1] TRUE
sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_CA.UTF-8
[5] LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_0.9.3.1 plyr_1.8 zoo_1.7-9 stringr_0.6.2
[5] reshape2_1.2.2
loaded via a namespace (and not attached):
[1] colorspace_1.2-2 dichromat_2.0-0 digest_0.6.3 grid_3.0.0
[5] gtable_0.1.2 labeling_0.1 lattice_0.20-15 MASS_7.3-26
[9] munsell_0.4 proto_0.3-10 RColorBrewer_1.0-5 scales_0.2.3
A.K.
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