[Rd] format.factor (PR#11512)

ripley at stats.ox.ac.uk ripley at stats.ox.ac.uk
Tue May 27 12:30:30 CEST 2008


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--27464147-1006481715-1211882711=:30017
Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-15; FORMAT=flowed
Content-Transfer-Encoding: 8BIT
Content-ID: <alpine.LFD.1.10.0805271109001.30017 at gannet.stats.ox.ac.uk>

>From the help page for factor

      For numeric or complex 'x', dims and dimnames are preserved on
      matrices/arrays and names on vectors: no other attributes are
      copied.

and that applies also to character 'x'.  Given that format[.default] is 
going to strip off all other attributes, the function can be simplified to

format.factor <- function (x, ...)
     format(structure(as.character(x), names=names(x),
                      dim=dim(x), dimnames=dimnames(x)), ...)

Will be changed in 2.7.0 patched shortly.


On Sun, 25 May 2008, oehl_list at gmx.de wrote:

> Dear all,
>
> differing from the standard behaviour of 'format' the current (2.7.0) 
> 'format.factor' destroys attributes like 'dim' and 'dimnames'. This 
> unfortunately breaks some general code in the new package 'ff' for large 
> file-based data which will support vectors and arrays of atomic and 
> factors.
>
> It would be nice if you could include the following fix in the next 
> release of R.
>
> Kind regards
>
> Jens Oehlschlägel
>
> format.factor <-
> function (x, ...){
>  a <- attributes(x)
>  a$class <- NULL
>  a$levels <- NULL
>  x <- as.character(x)
>  attributes(x) <- a
>  format(x, ...)
> }
>
>
>>   x <- factor(c("aa",letters[-1]))
>>   dim(x) <- c(13,2)
>>   format(x, justify="right")
> [1] "aa" " b" " c" " d" " e" " f" " g" " h" " i" " j" " k" " l" " m" " n" " o" " p" " q" " r" " s" " t" " u" " v" " w" " x" " y" " z"
>>
>>   format.factor <-
> +   function (x, ...){
> +     a <- attributes(x)
> +     a$class <- NULL
> +     a$levels <- NULL
> +     x <- as.character(x)
> +     attributes(x) <- a
> +     format(x, ...)
> +   }
>>
>>   format(x, justify="right")
>      [,1] [,2]
> [1,] "aa" " n"
> [2,] " b" " o"
> [3,] " c" " p"
> [4,] " d" " q"
> [5,] " e" " r"
> [6,] " f" " s"
> [7,] " g" " t"
> [8,] " h" " u"
> [9,] " i" " v"
> [10,] " j" " w"
> [11,] " k" " x"
> [12,] " l" " y"
> [13,] " m" " z"
>
>> version
>               _
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status
> major          2
> minor          7.0
> year           2008
> month          04
> day            22
> svn rev        45424
> language       R
> version.string R version 2.7.0 (2008-04-22)
>
> --
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at 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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
--27464147-1006481715-1211882711=:30017--



More information about the R-devel mailing list