[Rd] man page for as.matrix for data frames outdated?

Herve Pages hpages at fhcrc.org
Fri Nov 3 05:46:01 CET 2006


Hi again,


The man page for 'as.matrix' says:

     'as.matrix' is a generic function. The method for data frames will
     convert any non-numeric/complex column into a character vector
     using 'format' and so return a character matrix, except that
     all-logical data frames will be coerced to a logical matrix.

It's true that "all-logical data frames will be coerced to a logical
matrix":

    > fourLogicals <- 2:5>3
    > df1 <- data.frame(a=fourLogicals)
    > storage.mode(as.matrix(df1))
    [1] "logical"

Otherwise it's not true that 'as.matrix' will return a character matrix:

    > fourInts <- 2:-1
    > df2 <- data.frame(a=fourLogicals, b=fourInts)
    > storage.mode(as.matrix(df2))
    [1] "integer"


    > fourDoubles <- rep(pi,4)
    > df3 <- data.frame(c=fourDoubles, a=fourLogicals, b=fourInts)
    > storage.mode(as.matrix(df3))
    [1] "double"


    > fourComplexes <- (-1:2)+3i
    > df4 <- data.frame(a=fourLogicals, d=fourComplexes, b=fourInts,
    c=fourDoubles)
    > storage.mode(as.matrix(df4))
    [1] "complex"

If one column is of mode character, then 'as.matrix' will effectively
return a character matrix:

    > df5 <- data.frame(toto=c("a","bb"), titi=c(9,999))
    > storage.mode(as.matrix(df5))
    [1] "character"

Note that the doc says that "any non-numeric/complex column" will
be passed thru 'format' which seems to be exactly the other way
around:

    > as.matrix(df5)
      toto titi
    1 "a"  "  9"
    2 "bb" "999"

Anyway why one would like to have the numeric values passed
thru 'format' to start with?

This is in R-2.4.0 and recent R-devel.

Best,
H.




More information about the R-devel mailing list