[R] convert data frame of values into correlation matrix

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jan 30 20:46:19 CET 2010


Try his:

xtabs(corrs ~., myData)


On Sat, Jan 30, 2010 at 2:11 PM, Juliet Hannah <juliet.hannah at gmail.com> wrote:
> Hi Group,
>
> Consider a data frame like this:
>
> mylabel1 <- rep(c("A","B","C"),each=3)
> mylabel2 <- rep(c("A","B","C"),3)
> corrs <- c(1,.8,.7,.8,1,.7,.7,.7,1)
>  myData <- data.frame(mylabel1,mylabel2,corrs)
>
> myData
>
>  mylabel1 mylabel2 corrs
> 1        A        A   1.0
> 2        A        B   0.8
> 3        A        C   0.7
> 4        B        A   0.8
> 5        B        B   1.0
> 6        B        C   0.7
> 7        C        A   0.7
> 8        C        B   0.7
> 9        C        C   1.0
>
> I would like to find a general way to get this matrix from the above dataframe.
>
> corrmat <- matrix(corrs,nrow=3,byrow=TRUE)
> row.names(corrmat) <- c("A","B","C")
> colnames(corrmat) <- c("A","B","C")
> corrmat
>    A   B   C
> A 1.0 0.8 0.7
> B 0.8 1.0 0.7
> C 0.7 0.7 1.0
>
> The solution I have is the one above where I rearrange the data so
> that I can just use matrix() on one
> of the columns. I am looking for a solution in which I don't have to do this.
>
> Thanks,
>
> Juliet



More information about the R-help mailing list