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.
Martin Maechler
2006-Nov-03 08:26 UTC
[Rd] man page for as.matrix for data frames outdated?
>>>>> "Herve" == Herve Pages <hpages at fhcrc.org> >>>>> on Thu, 02 Nov 2006 20:46:01 -0800 writes:Herve> 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. In very old versions of R (e.g. 0.3 from March 1996), is.numeric(<logical>) was TRUE and there the help page was entirely correct. I think if you replace [in the above paragraph] "non-numeric/complex" by "non-(logical/numeric/complex)" the help page is correct again. Herve> If one column is of mode character, then 'as.matrix' Herve> will effectively return a character matrix: (as it says in the man page, and always did in all implementations of the S language) Herve> Note that the doc says that "any non-numeric/complex Herve> column" will be passed thru 'format' which seems to Herve> be exactly the other way around: No! You left off the second part of the statement cited initally. Slightly reformulated: Iff there's any non-(logical/numeric/complex) column, that will have to be passed through format and hence the result must be a character matrix and hence every other column also needs to be "formatted" >> as.matrix(df5) Herve> toto titi 1 "a" " 9" 2 "bb" "999" Herve> Anyway why one would like to have the numeric values Herve> passed thru 'format' to start with? Recall: The result must be a matrix ! If it can't be a numeric matrix the decision was it must be character. BTW: Exactly because of your problems, The function data.matrix() had been devised (long ago in pre-R times), and data.matrix *is* the first entry in "See Also" on the help page for as.matrix Martin
Maybe Matching Threads
- Reshaping data with xtabs giving me 'extra' data
- splitting into multiple dataframes and then create a loop to work
- create data frame(s) from a list with different numbers of rows
- How to get a specific named element in a nested list
- Windows 2000 crash while using rbind (PR#8225)