An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080327/13dfc3fb/attachment.pl
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:
Hello:
I have a tab delim file with 100 rows and 100 columns.
I have numerical values in this table. What I want is to create an image
color map with color gradation.
my values range from -5 to 0. max value is 0.
to acheive this, I need to convert my data.frame into matrix.
I tried following :
mydf <- read.table('mytable',sep='\t',header=T)
mydf1 <- mydf[,2:100]
colnames(mydf1) <- as.character(mydf[,1])
here comes the trouble:
mymat <- as.matrix(mydf1)
-- numerical values become character values. -1 becomes "-1"
mymat <- data.matrix(mydf1)
-- whole matrix is filled with 1,2 and 3.
Is there any other way I can do this.
say row names are city schools
colnames are student names
appreciate your help.
Thanks
AJ
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Adrian Johnson wrote:
Hello:
I have a tab delim file with 100 rows and 100 columns.
I have numerical values in this table. What I want is to create an image
color map with color gradation.
my values range from -5 to 0. max value is 0.
to acheive this, I need to convert my data.frame into matrix.
I tried following :
mydf <- read.table('mytable',sep='\t',header=T)
mydf1 <- mydf[,2:100]
colnames(mydf1) <- as.character(mydf[,1])
here comes the trouble:
mymat <- as.matrix(mydf1)
-- numerical values become character values. -1 becomes "-1"
mymat <- data.matrix(mydf1)
-- whole matrix is filled with 1,2 and 3.
Is there any other way I can do this.
say row names are city schools
colnames are student names
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:
Adrian Johnson wrote:
Hello: I have a tab delim file with 100 rows and 100 columns. I have numerical values in this table. What I want is to create an
image Probably not. See below.
color map with color gradation.
my values range from -5 to 0. max value is 0.
to acheive this, I need to convert my data.frame into matrix.
I tried following :
mydf <- read.table('mytable',sep='\t',header=T)
mydf1 <- mydf[,2:100]
colnames(mydf1) <- as.character(mydf[,1])
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]
here comes the trouble: mymat <- as.matrix(mydf1) -- numerical values become character values. -1 becomes "-1"
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.
mymat <- data.matrix(mydf1) -- whole matrix is filled with 1,2 and 3. Is there any other way I can do this. say row names are city schools colnames are student names
Hi Adrian, Have you looked at color2d.matplot in the plotrix package? Jim
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
> Is there any other way I can do this. > > say row names are city schools > colnames are student names >
Hi Adrian, Have you looked at color2d.matplot in the plotrix package? Jim
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