[R] as.matrix does not turn data frame into character matrix
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Oct 17 08:45:22 CEST 2003
At least in R 1.8.0 it is a list matrix:
> class(jank)
[1] "matrix"
> typeof(jank)
[1] "list"
The help page is not quite correct, as it does not mention what happens
when you create a data frame with a *list* as a column.
How did you create this data frame? Columns cortrange and logcortrange
are surely intended to be numeric columns, and it is pretty moot point if
it is a valid data frame (it should not be possible to create it with
data.frame, for example, and do.call("data.frame", unclass(junk)) fails).
Please note that R 1.8.0 is current: we are nowhere near` 7.0 or `7.0'.
A lot of error-checking/correction in the data.frame area was added in
1.8.0.
On Thu, 16 Oct 2003, Jacob Wegelin wrote:
>
> The as.matrix function behaves in a puzzling manner. The help file says:
> "`as.matrix' is a generic function. The method for data frames will
> convert any non-numeric column into a character vector using
> `format' and so return a character matrix."
> But this does not appear to be the case in the following example. Instead,
> as.matrix turns a data.frame into a list, not a character matrix, which
> wreaks havoc with my old code.
>
> junk<-
> structure(list(SUBNUM = structure(c(3, 4, 5, 7, 6), class = "factor",
> .Label = c("01",
> "02", "03", "04", "07", "08", "09", "10", "11", "12", "13", "16",
> "17", "18", "21", "22", "23", "24", "25", "26", "27", "28")),
> AGE = c(7, 7, 10, 8, 5), DIAGNOSI = c(1, 1, 1, 1, 1), cortrange =
> structure(list(
> "03" = 19.0674, "04" = 40.3009, "07" = 37.0205, "09" = 8.84131,
> "08" = 10.9855), .Names = c("03", "04", "07", "09", "08"
> )), logcortrange = structure(list("03" = 1.90097866386896,
> "04" = 2.75040785570225, "07" = 3.15633470025647, "09" =
> 2.56744094585387,
> "08" = 2.84160608522206), .Names = c("03", "04", "07",
> "09", "08"))), .Names = c("SUBNUM", "AGE", "DIAGNOSI", "cortrange",
> "logcortrange"), row.names = c("03", "04", "07", "09", "08"), class =
> "data.frame")
>
> > junk
> SUBNUM AGE DIAGNOSI cortrange logcortrange
> 03 03 7 1 19.0674 1.900979
> 04 04 7 1 40.3009 2.750408
> 07 07 10 1 37.0205 3.156335
> 09 09 8 1 8.84131 2.567441
> 08 08 5 1 10.9855 2.841606
>
> > jank<-as.matrix(junk)
> > jank
> SUBNUM AGE DIAGNOSI cortrange logcortrange
> 03 "03" 7 1 19.0674 1.900979
> 04 "04" 7 1 40.3009 2.750408
> 07 "07" 10 1 37.0205 3.156335
> 09 "09" 8 1 8.84131 2.567441
> 08 "08" 5 1 10.9855 2.841606
>
> Notice that the first column is character, whereas the other columns are plain numeric!
> This is *not* a matrix of character.
>
> > dput(jank)
> structure(list("03", "04", "07", "09", "08", 7, 7, 10, 8, 5,
> 1, 1, 1, 1, 1, 19.0674, 40.3009, 37.0205, 8.84131, 10.9855,
> 1.90097866386896, 2.75040785570225, 3.15633470025647, 2.56744094585387,
> 2.84160608522206), .Dim = c(5, 5), .Dimnames = list(c("03",
> "04", "07", "09", "08"), c("SUBNUM", "AGE", "DIAGNOSI", "cortrange",
> "logcortrange")))
>
> Is this a bug?
>
> One result of this:
>
> > cbind(dimnames(jank)[[1]], jank)
> Error in cbind(...) : cannot create a matrix from these types
>
> (I'm using version 7.0, because the links for downloading version 7.1 are dead today:)
>
> > version
> _
> platform i386-pc-mingw32
> arch i386
> os mingw32
> system i386, mingw32
> status
> major 1
> minor 7.0
> year 2003
> month 04
> day 16
> language R
>
> Thanks for any information.
>
> Jake
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list