as.matrix.data.frame() modifies content of character columns (PR#386)

jens.oehlschlaegel-akiyoshi@mdfactory.de jens.oehlschlaegel-akiyoshi@mdfactory.de
Wed, 22 Dec 1999 16:23:00 +0100 (MET)



If you have the following dataframe:

> x <- data.frame(x=I(rep('"', 3)))

then

> as.matrix(x)[1,1] == x[1,1]
[1] FALSE

which seems to be WRONG to me.

Also I don't understand
> x
     x
1 \\\"
2 \\\"
3 \\\"


Details
=======

As expected
> unclass(x)
$x
[1] "\"" "\"" "\""
attr(,"class")
[1] "AsIs"

attr(,"row.names")
[1] "1" "2" "3"


but now look at this
correct:
> x[1,1]
[1] "\""

wrong:
> as.matrix(x)[1,1]
[1] "\\\""

this is not only a printing problem
correct:
> cat(x[1,1], "\n")
"

wrong:
> cat(as.matrix(x)[1,1], "\n")
\"


Interpretation
==============

To my understanding, this is caused, because as.matrix.data.frame makes use
of format, and format does

> cat(format('"'), "\n")
\"

If this is the cause and format() does right, then consequently

         format MUST NEVER be used within R routines EXCEPT FOR PRINTING,
         i.e. formatting something and storing it or returning it from a
function is dangerous.
         Even the use for cat() may be dangerous, as cat() as a side effect
may store data,
         as in write() or write.table(). This deserves a BIG warning in the
documentation of format.

Here is a list of functions of package:base making use of format

> collect <- character()
> for (i in ls("package:base")){
+   if (   any(grep("format[(]", deparse(get(i, pos="package:base"))))
+       || any(grep("format.default[(]", deparse(get(i,
pos="package:base"))))
+      )collect <- c(collect, i)
+ }
> collect
 [1] "add1.default"          "add1.glm"              "add1.lm"
"all.equal.numeric"
 [5] "anovalist.lm"          "as.matrix.data.frame"  "drop1.default"
"drop1.glm"
 [9] "drop1.lm"              "format.char"           "format.default"
"format.pval"
[13] "help.search"           "hist.default"          "legend"
"ls.print"
[17] "print.aov"             "print.aovlist"         "print.coefmat"
"print.dummy.coef"
[21] "print.glm"             "print.glm.null"        "print.htest"
"print.lm"
[25] "print.mtable"          "print.summary.glm"     "print.summary.lm"
"print.summary.lm.null"
[29] "print.tables.aov"      "print.ts"              "quantile.default"
"step"
[33] "str.default"           "summary.aov"           "summary.data.frame"
"summary.default"
[37] "summary.infl"          "symnum"



platform Windows
arch     x86
os       Win32
system   x86, Win32
status
major    0
minor    90.1
year     1999
month    December
day      15
language R


--
Dr. Jens Oehlschlägel-Akiyoshi
MD FACTORY GmbH
Bayerstrasse 21

80335 München

Tel.: 089 545 28-27
Fax.: 089 545 28-10
http://www.mdfactory.de

Standard Disclaimers: Opinions expressed here are personal
and are not otherwise represented.



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._