[R] Odp: Letters(V1, V2..) as row col indices instead of numbers

Petr PIKAL petr.pikal at precheza.cz
Fri Jan 28 17:23:32 CET 2011


Hi

KumaraGuru <kumaragurutp at gmail.com> napsal dne 28.01.2011 17:03:50:

> I want to melt the cor matrix and form a 3 col matrix with row# col# and 
cor. 

Well, in that case it will be probably better to transform it to data 
frame and use melt. Or you can drop dimensions of this matrix and generate 
proper sequences of row and column numbers. Try it with smaller matrix.

Row names does not matter

> x<-matrix(1:12, 4,4)
> row.names(x)<-letters[1:4]
> colnames(x)<-letters[1:4]
> x
  a b  c d
a 1 5  9 1
b 2 6 10 2
c 3 7 11 3
d 4 8 12 4
> dim(x)<-NULL
> x
 [1]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4
> cbind(rep(1:4, 4), rep(1:4, each=4), x)
           x
 [1,] 1 1  1
 [2,] 2 1  2
 [3,] 3 1  3
 [4,] 4 1  4
 [5,] 1 2  5
 [6,] 2 2  6
 [7,] 3 2  7
 [8,] 4 2  8
 [9,] 1 3  9
[10,] 2 3 10
[11,] 3 3 11
[12,] 4 3 12
[13,] 1 4  1
[14,] 2 4  2
[15,] 3 4  3
[16,] 4 4  4
> 



Regards
Petr


> 
> This I m using to draw a graph. 
> 
> Regards,
> Kumaraguru
> 
> 
> On Jan 28, 2011, at 6:47 AM, Petr PIKAL <petr.pikal at precheza.cz> wrote:
> 
> > Hi
> > 
> > r-help-bounces at r-project.org napsal dne 28.01.2011 09:52:05:
> > 
> >> 
> >> What makes matrix to print V1, V2 as the row and col indices instead 
of
> >> numbers??
> >> 
> >> cdata = read.table("ramesh.txt") #cdata is read from a file.
> >> c1 = cor(cdata) 
> >> 
> >> I am printing c1 below. I need only numbers. I dont need V1, 
V2...what
> >> should I do in this case??
> >> http://r.789695.n4.nabble.com/file/n3243980/ramesh.txt ramesh.txt 
> >> 
> >>           V1        V2        V3        V4        V5        V6 V7
> >> V1  1.0000000 0.9471591 0.9191233 0.9403129 0.9689690 0.9621362 
> > 0.9529811
> >> V2  0.9471591 1.0000000 0.8888108 0.9108174 0.9703906 0.9425248 
> > 0.9294308
> >> V3  0.9191233 0.8888108 1.0000000 0.8788107 0.9077245 0.9078971 
> > 0.8965331
> >> V4  0.9403129 0.9108174 0.8788107 1.0000000 0.9477058 0.9017977 
> > 0.8646251
> >> V5  0.9689690 0.9703906 0.9077245 0.9477058 1.0000000 0.9531459 
> > 0.9162016
> >> V6  0.9621362 0.9425248 0.9078971 0.9017977 0.9531459 1.0000000 
> > 0.9447130
> >> V7  0.9529811 0.9294308 0.8965331 0.8646251 0.9162016 0.9447130 
> > 1.0000000
> >> V8  0.9167526 0.9395067 0.8491496 0.8942693 0.9460702 0.9304229 
> > 0.8505287
> >> V9  0.9512951 0.9598302 0.8666839 0.9465725 0.9645178 0.9486105 
> > 0.8979753
> >> V10 0.9551633 0.9040160 0.9180022 0.9429816 0.9448644 0.9220875 
> > 0.9075537
> > 
> > Vn are names of columns which R gave to your matrix when you read it 
as a 
> > data frame. And cor just makes a matrix of pairwise corelation 
> > coefficients from this data frame conveniently named from this data 
frame. 
> > If you do not want those names just unname it.
> > 
> > c1 = unname(cor(cdata))
> > 
> > But I wonder why you dislike those names which are,  well, just names.
> > 
> > Regards
> > Petr
> > 
> > 
> >> 
> >> -- 
> >> View this message in context: 
> > http://r.789695.n4.nabble.com/Letters-V1-V2-as-
> >> row-col-indices-instead-of-numbers-tp3243980p3243980.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >> 
> >> ______________________________________________
> >> 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.
> >



More information about the R-help mailing list