From murdoch at stats.uwo.ca  Wed Jun  1 20:56:20 2005
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Wed Jun  1 20:56:23 2005
Subject: [Rd] format.data.frame (was: [R] sink() within a function?)
In-Reply-To: <e2b7f8676f0c63a013af6b00e8f79466@sandia.gov>
References: <200506011623.j51GNPv9007461@erdos.math.unb.ca>
	<a2fb2c317d530f08762e83f792dbb1bf@sandia.gov>
	<429DEEE2.5080400@stats.uwo.ca>
	<e2b7f8676f0c63a013af6b00e8f79466@sandia.gov>
Message-ID: <429E04D4.1080307@stats.uwo.ca>

Jon Stearley wrote:
> On Jun 1, 2005, at 11:22 AM, Duncan Murdoch wrote:
> 
>>     These functions convert their first argument to a vector (or
>>     array) of character strings which have a common format (as is done
>>     by 'print'), fulfilling 'length(format*(x, *)) == length(x)'.  The
>>     trimming with 'trim = TRUE' is useful when the strings are to be
>>     used for plot 'axis' annotation.
> 
> 
> i saw this but
>    class(x)    # [1] "data.frame"
>    y<-format(x)
>    class(y)    # [1] "data.frame"
> confused me, let alone y<-as.character(format(x)).  i'm still an R 
> newbie...
> 
> 
>>I'll try to make it clearer.
> 

I think you've got a right to be confused, newbie or not.
format.data.frame() doesn't seem to follow the documentation, either 
before or after my change to the docs.  The result of format(x) is not a 
vector or array or even a data.frame of character strings, it's a 
data.frame of factors.

I'm not sure this is a reasonable thing to do.  Does anyone else have
an opinion on this?

My initial feeling is that format() on a data.frame should return a 
data.frame of character vectors, it shouldn't convert them to factors. 
One should be able to expect that format(x)[1,1] gives a character 
value, rather than the underlying factor encoding as it does in this 
example:

 > x <- data.frame(a=rnorm(5), b=rnorm(5))
 > y <- format(x, digits=3)
 > y
        a      b
1 -1.007 -0.525
2 -0.570  1.128
3  0.162  1.729
4 -1.642 -0.485
5  0.381  0.621
 > cat(y[1,1],"\n")
2

Duncan Murdoch


[Rd] format.data.frame (was: [R] sink() within a function?)
Duncan Murdoch