[Rd] Subset of a 'table' divided by a 'table' is a 'table', but printed by 'print.default'

Duncan Murdoch murdoch.duncan at gmail.com
Tue Apr 30 15:36:39 CEST 2013

On 13-04-30 3:38 AM, Suharto Anggono Suharto Anggono wrote:
> This is just info.
> I recently got something like this.
>> x <- factor(c("A","A","B","B"), levels=c("A","B"))
>> y <- factor(c("a","b","a","b"), levels=c("a","b"))
>> table(x, y)[, "a"] / table(x)
> x
>    A   B
> 0.5 0.5
> attr(,"class")
> [1] "table"
> The printing indicates that the result is of class 'table'. But, the 'print' method of class 'table' does not print attr(,"class"). It seems that 'print.default' is used in the printing. I am OK with it, just unusual.

This is fixed as of r62697 in R-devel and R-patched.

The documentation in ?Arithmetic was also inaccurate, and has been 
modified.  The correct description is now:

   The rules for determining the attributes of the result are rather
   complicated.  Most attributes are taken from the longer argument.
   Names will be copied from the first if it is the same length as the
   answer, otherwise from the second if that is.  If the arguments are
   the same length, attributes will be copied from both, with those of
   the first argument taking precedence when the same attribute is
   present in both arguments. For time series, these operations are
   allowed only if the series are compatible, when the class and
   \code{\link{tsp}} attribute of whichever is a time series (the same,
   if both are) are used.  For arrays (and an array result) the
   dimensions and dimnames are taken from first argument if it is an
   array, otherwise the second.

This was true for R 3.0.0 and earlier as well, but before my patch while 
the class was copied from the second argument, the object bit was copied 
from the first argument.  Now things are consistent.

Duncan Murdoch

> I think, that is another symptom of an already known behavior as in the following:
> - PR#2345: difftime arithmetic
> - PR#13209: S4 object does not commute?
>> sessionInfo()
> R version 3.0.0 (2013-04-03)
> Platform: i386-w64-mingw32/i386 (32-bit)
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [5] LC_TIME=English_United States.1252
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

More information about the R-devel mailing list