[R] matrix from list

Martin Maechler maechler at stat.math.ethz.ch
Sat Apr 26 11:21:59 CEST 2008

>>>>> "OL" == Olivier Lefevre <lefevrol at yahoo.com>
>>>>>     on Sat, 26 Apr 2008 01:31:16 +0200 writes:

    OL> Greg Snow wrote:
    >> The '[[' only returns a single element from a data structure

    OL> I know but that is precisely what I find arbitrary.

    OL> Anyway you are right that it would still return the kind of object, only 
    OL> subsetted, which is not I want. As someone kindly pointed out to me 
    OL> offline, besides the unlist trick posted before, you can also achieve the 
    OL> desired result with

    OL> m <- as.matrix(l)
    OL> dimnames(m) <- NULL

Two remarks on these two lines:

- Slightly better for the 99% user- case of coercing a
  'data.frame' (the result of read.table() or similar) ato a matrix is

       m <- data.matrix(.)

  The difference to as.matrix() is that data.matrix() also
  produces a numeric matrix in the case the data frame contains


	  (dd <- data.frame(x= rnorm(10), f = gl(2,5))

	  as.matrix(dd)   ## not what you want
	  data.matrix(dd)	## what you want

- I don't understand why you want to throw away the dimnames of
  the resulting numeric matrix.  Rather, in general you'd want
  to keep them.

Martin Maechler, ETH Zurich

More information about the R-help mailing list