Skip to content
Prev 139299 / 398506 Next

Converting a data frame with values into a matrix/

The way that you have set up the data.frame is rather
unusual. It makes NES a factor which I suspect you
don't want. Do str(xx) to see what I mean

Here is a way that I think does what you want but with
the data.frame constructed in a different manner. 
Again do str(aa) to see the difference
=====================================================
aa <- data.frame(Name=c( "Mike" ,"Carl", "Gene",
"James","Dough"),
         Class=c(  "A",  "A",  "C",  "A",  "B"),
         NES=c( 0.01, 0.2, 0.3, -0.3, 0) )  
aa
         
library(reshape)
mm  <- melt(aa, id=c("Name", "Class"))
mm1  <- cast(mm, Class~Name)

aba <- mm1[,2:6]
aba[is.na(aba)] <- 1

final <- data.frame(mm1$Class, aba)
names(final) <- names(mm1) 
final

===================================================
--- Srinivas Iyyer <srini_iyyer_bio at yahoo.com> wrote:

            
matrix(1,length(unique(xx$Class)),length(unique(xx[,1])))