Bug 17147 - xtabs(~ exclude) fails
Summary: xtabs(~ exclude) fails
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Analyses (show other bugs)
Version: R 3.3.0
Hardware: ix86 (32-bit) Windows 32-bit
: P5 minor
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-09-10 05:13 UTC by Suharto Anggono
Modified: 2016-10-19 10:09 UTC (History)
1 user (show)

See Also:


Attachments
Against R devel r71527 (1.16 KB, patch)
2016-10-17 16:15 UTC, Suharto Anggono
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Suharto Anggono 2016-09-10 05:13:48 UTC
Example:

R> exclude <- exclude2 <- c(TRUE, FALSE)
R> xtabs(~ exclude)   # fails
Error in table(exclude = c(2L, 1L)) : nothing to tabulate
R> xtabs(~ exclude2)   # succeeds
exclude2
FALSE  TRUE
    1     1
R> xtabs(rep(1, length(exclude)) ~ exclude)   # succeeds
exclude
FALSE  TRUE
    1     1
R> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows XP (build 2600) Service Pack 2

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


Using table(by) instead of do.call("table", by) in the code of function 'xtabs' is a fix.
Comment 1 Suharto Anggono 2016-10-17 16:15:41 UTC
Created attachment 2171 [details]
Against R devel r71527
Comment 2 Martin Maechler 2016-10-18 15:11:56 UTC
(In reply to Suharto Anggono from comment #1)
> Created attachment 2171 [details]
> Against R devel r71527

Thank you, Suharto.  
 
Your change to  table() {in addition to the one to xtabs() which you mentioned before .. and which we really should have applied long ago} 
may change the semantics, as in that "length(args) == 1" case,
as in that case, the internal  list.names() function won't be called, but the more simple treatment used instead.

Are you sure this will always give the same as before?
Comment 3 Martin Maechler 2016-10-18 16:59:46 UTC
(In reply to Martin Maechler from comment #2)
> (In reply to Suharto Anggono from comment #1)
> > Created attachment 2171 [details]
> > Against R devel r71527
> 
> Thank you, Suharto.  
>  
> Your change to  table() {in addition to the one to xtabs() which you
> mentioned before .. and which we really should have applied long ago} 
> may change the semantics, as in that "length(args) == 1" case,
> as in that case, the internal  list.names() function won't be called, but
> the more simple treatment used instead.
> 
> Are you sure this will always give the same as before?

I have decided to commit the non- (or 'less-') controversial part, i.e., just the originally proposed change from   do.call("table", by)  to   table(by)
in xtabs() definition in  svn rev 71533,
with the idea to port it to R-patched at about the time it becomes "R 3.3.2 beta".

The change to table() itself seems only vaguely related to this bug fix,
and would go in to R-devel only  (and after hearing more about it).

Thank you once more,
Martin
Comment 4 Suharto Anggono 2016-10-18 17:18:32 UTC
(In reply to Martin Maechler from comment #2)
> (In reply to Suharto Anggono from comment #1)
> > Created attachment 2171 [details]
> > Against R devel r71527
> 
> Thank you, Suharto.  
>  
> Your change to  table() {in addition to the one to xtabs() which you
> mentioned before .. and which we really should have applied long ago} 
> may change the semantics, as in that "length(args) == 1" case,
> as in that case, the internal  list.names() function won't be called, but
> the more simple treatment used instead.
> 
> Are you sure this will always give the same as before?

As long as all arguments in '...' are named (no "" in names), the internal function 'list.names' would return argument names. So, assuming that there is no "" in names(by), 'xtabs' would be as before when it is correct.

The change to 'table' is only for '...' that is a single list argument. The change makes the behavior of 1-dimensional table like table with dimension 2 or more when 'dnn' is unspecified.

If table(by) is used in 'xtabs' without changing 'table', the label (names of dimnames) in the result of
xtabs(~ exclude2)
is "by", not "exclude2".

Without changing 'table',
table(by, dnn = names(by))
can be used in 'xtabs'.
Comment 5 Martin Maechler 2016-10-19 10:09:13 UTC
(In reply to Suharto Anggono from comment #4)
....
 
> If table(by) is used in 'xtabs' without changing 'table', the label (names
> of dimnames) in the result of
> xtabs(~ exclude2)
> is "by", not "exclude2".
> 
> Without changing 'table',
> table(by, dnn = names(by))
> can be used in 'xtabs'.

You are right, and I was wrong that the table change was unrelated;
thank you!

I'm using table(by, dnn = *)   ---> svn rev 71536