Skip to content
Prev 140585 / 398506 Next

Converting a data frame to matrix

Hi

r-help-bounces at r-project.org napsal dne 28.03.2008 10:18:20:
image

Probably not. See below.
It should be probably 

rownames(mydf1) <- as.character(mydf[,1])

mydf<-rnorm(100*100)
mydf<-matrix(mydf, 100,100)
mydf<-data.frame(mydf)
mydf1<-mydf[,2:100]
colnames(mydf1)<-as.character(mydf[,1])
Error in names(x) <- value : 
  'names' attribute [100] must be the same length as the vector [99]
If you had all values in data frame really numeric it shall stay as 
numeric also in your matrix. Try

str(mydf1)

and you will probably find out that some variables are factors/character. 
Therefore as.matrix transfers **all** values to character as matrix can 
not mix data of various type.
http://www.R-project.org/posting-guide.html