[R] Converting 3 columns of a data frame into a matrix

Jeff08 jefferyding at gmail.com
Tue Jun 22 05:24:12 CEST 2010


df<-data.frame()
df[1:8,1]<-c("1","2","5","3","1","4","3","5") ##identifier 1
df[1:8,2]<-c("c","a","b","c","a","b","b","a") ##identifier 2
df[1:8,3]<-c(1,2,3,4,5,6,7,8) ##value

##Each unique combination of identifiers identifies a datapoint


##What I am trying to do is create a matrix with values of the third column
## and rows/column names of the 1st and 2nd column. As you can see, a lot of
## combinations of identifier1 and identifier 2, for example (1,b), do not
## have a value attached to them, and I would like those values in the
matrix
## to be NA.

##From the ecodist, I have tried crosstab(df[,1],df[,2],df[,3])
##It works perfectly in this case, but when I use it on my actual data.frame
##It ALWAYS crashes..differences between my real data.frame
## 1)Actual df is 3 million rows, 2)df[,1] and df[,2] are
## of class "factor" rather than class "character"
## knowing this I have tried as.character(df[,1]) & same for df2
##but to no avail
-- 
View this message in context: http://r.789695.n4.nabble.com/Converting-3-columns-of-a-data-frame-into-a-matrix-tp2263540p2263540.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list