Suharto Anggono Suharto Anggono
2013-Apr-30 07:38 UTC
[Rd] Subset of a 'table' divided by a 'table' is a 'table', but printed by 'print.default'
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. 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 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base
Duncan Murdoch
2013-Apr-30 09:49 UTC
[Rd] Subset of a 'table' divided by a 'table' is a 'table', but printed by 'print.default'
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. > > 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?There are no S4 objects or time objects involved here, so it's likely something different. I notice the following strange behaviour. If I save the ratio in a variable, it prints differently with an explicit print than with auto printing: x <- factor(c("A","A","B","B"), levels=c("A","B")) y <- factor(c("a","b","a","b"), levels=c("a","b")) ratio <- table(x, y)[, "a"] / table(x) ratio # shows what you saw print(ratio) # shows what you expected Something else that's a little strange: the numerator does not have class "table", so you can get the same results with ratio <- c(1,1) / table(x) Looking at the internals, I see that the ratio variable has a class attribute, but does not have its "object bit" set. So it looks like the bug is in the implementation of "/". Either it should drop the class, or it should set the object bit. The difference in printing between auto-printing and explicit printing may be worth addressing, but really objects like ratio shouldn't exist, so it's not surprising that they behave strangely. Duncan Murdoch> > >> 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 > [4] LC_NUMERIC=C > [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 >
Barry Rowlingson
2013-Apr-30 10:51 UTC
[Rd] Subset of a 'table' divided by a 'table' is a 'table', but printed by 'print.default'
On Tue, Apr 30, 2013 at 10:49 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> So it looks like the bug is in the implementation of "/". Either it > should drop the class, or it should set the object bit. > > The difference in printing between auto-printing and explicit printing > may be worth addressing, but really objects like ratio shouldn't exist, > so it's not surprising that they behave strangely.Also happens with *, if LHS is not a scalar: > c(1,1)*table(x) x A B 2 2 attr(,"class") [1] "table" > 1*table(x) x A B 2 2 - similar with + and -, but not unary + and -. Nice to see a bug in arithmetic operators that isn't covered by everyone screaming "FAQ 7.31!!!" Barry
Duncan Murdoch
2013-Apr-30 13:36 UTC
[Rd] Subset of a 'table' divided by a 'table' is a 'table', but printed by 'print.default'
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 > [4] LC_NUMERIC=C > [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 >