Skip to content

Converting a data frame to matrix

5 messages · Adrian Johnson, jim holtman, Jim Lemon +2 more

#
Do an 'str' on your data so that we see what it contains.  It may
appear to be numbers, but they might also be factors as a result of
the read.table.

On Thu, Mar 27, 2008 at 7:43 PM, Adrian Johnson
<oriolebaltimore at gmail.com> wrote:

  
    
#
Adrian Johnson wrote:
Hi Adrian,
Have you looked at color2d.matplot in the plotrix package?

Jim
#
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
#
You might also want to check out the seriation package, which provides
tools to reorder your matrix to better reveal any patterns in the
data.

Hadley