Juliet Hannah
2010-Jan-30 19:11 UTC
[R] convert data frame of values into correlation matrix
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
David Winsemius
2010-Jan-30 19:41 UTC
[R] convert data frame of values into correlation matrix
On Jan 30, 2010, at 2:11 PM, Juliet Hannah 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.Possibilities: >> reshape(myData, timevar="mylabel2", direction="wide", v.names="corrs", idvar="mylabel1")[,2:4] corrs.A corrs.B corrs.C 1 1.0 0.8 0.7 4 0.8 1.0 0.7 7 0.7 0.7 1.0 #--------- > unstack(myData, corrs ~ mylabel2 ) A B C 1 1.0 0.8 0.7 2 0.8 1.0 0.7 3 0.7 0.7 1.0 #------- library(reshape) mdat <- melt(myData) cast(mdat, mylabel1 ~ mylabel2 ) mylabel1 A B C 1 A 1.0 0.8 0.7 2 B 0.8 1.0 0.7 3 C 0.7 0.7 1.0 -- . David Winsemius, MD Heritage Laboratories West Hartford, CT
Gabor Grothendieck
2010-Jan-30 19:46 UTC
[R] convert data frame of values into correlation matrix
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