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

Duncan Murdoch murdoch at stats.uwo.ca
Wed Jun 1 20:56:20 CEST 2005


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



More information about the R-devel mailing list