[R] Converting dataframe to matrix

Philipp Pagel p.pagel at wzw.tum.de
Fri Oct 16 10:51:45 CEST 2009


On Fri, Oct 16, 2009 at 01:33:14AM -0700, Noah Silverman wrote:
> Hi,
> 
> I'm experimenting with a few learners that require a matrix as their
> input.  (Currently svmpath, vbmp, etc.)
> 
> I currently have a dataframe with 50 columns and 20,000 rows.
> 
> I tried using:
> 
> x <- as.matrix(my_data.frame)
> 
> If I then as, "is.matrix(x)", I get TRUE.
> 
> However everywhere I've tried to use the matrix returns errors.

Without more information I can't even start to guess what is going
wrong. Please give a short, reproducible example of what you did and
what errors you encountered.

as.matrix() should suffice for creating a matrix from a data.frame :

> foo <- data.frame(1:4, 4:1, sqrt(1:4), log(4:1))
> foo
  X1.4 X4.1 sqrt.1.4.  log.4.1.
  1    1    4  1.000000 1.3862944
  2    2    3  1.414214 1.0986123
  3    3    2  1.732051 0.6931472
  4    4    1  2.000000 0.0000000
> det(foo)
Error in UseMethod("determinant") :
  no applicable method for "determinant"
> det(as.matrix(foo))
[1] -0.1092489

So probably your problem is somewhere else.

cu
	Philipp

-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
Freising, Germany
http://webclu.bio.wzw.tum.de/~pagel/




More information about the R-help mailing list