Re: BUG in format()? (PR#383)

About this list Date view Thread view Subject view Author view Other groups

Subject: Re: BUG in format()? (PR#383)
From: Prof Brian D Ripley (ripley@stats.ox.ac.uk)
Date: Wed 22 Dec 1999 - 04:53:45 EST


Message-ID: <Pine.GSO.4.05.9912211832220.6587-100000@auk.stats>

On Tue, 21 Dec 1999 jens.oehlschlaegel-akiyoshi@mdfactory.de wrote:

>
>
> In RW0.651 and RW0.901,
> I discovered some unexpected behaviour when I used as.matrix.data.frame()
>
> > matrix('"', 2, 2)
> [,1] [,2]
> [1,] "\"" "\""
> [2,] "\"" "\""
> > unclass(as.data.frame(matrix('"', 2, 2)))
> $V1
> [1] "\"" "\""
>
> $V2
> [1] "\"" "\""
>
> attr(,"row.names")
> [1] "1" "2"

Now that is unexpected. The components should be factors. The code
converting matrices to data frames is not behaving as the prototype,
and that explains the rest, as the prototype gets the answer you would
expect. The rule is supposed to be that adding a matrix is equivalent to
adding each column in turn (White Book, page 59, and the S code).

I am not aware of any documentation of an R difference here (but I may be
wrong, although I checked the FAQ), and we need to fix either
as.data.frame.matrix or as.matrix.data.frame. I will argue for the first.

> > as.matrix(as.data.frame(matrix('"', 2, 2)))
> V1 V2
> 1 "\\\"" "\\\""
> 2 "\\\"" "\\\""
>
>
> I traced it back to a call to format (probably .Internal(format, ...))
>
> > format('"')
> [1] "\\\""
>
> > format('\\')
> [1] "\\\\"
>
> Same in format.char() but not in formatC()
>
> This behaviour of format() is not intended, is it?

I suspect it is. What is going on here is that you are not expected to
feed the results of format to print, but to cat etc. formatC both formats
and prints. Compare

> zz <- '"'
> zzz <- format(zz)
> print(zz)
[1] "\""
> print(zzz)
[1] "\\\""
> cat(zz, "\n")
"
> cat(zzz, "\n")
\"

The prototype does the same. One can argue that the conventions
are sub-optimal, but that is what R-like languages do.

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Other groups

This archive was generated by hypermail 2b25 : Tue 04 Jan 2000 - 14:16:11 EST