Bug 17200 - Wishlist: make 'pmax' involving 0-length classed object returns length 0
Summary: Wishlist: make 'pmax' involving 0-length classed object returns length 0
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Wishlist (show other bugs)
Version: R 3.3.0
Hardware: All All
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2016-12-25 03:01 UTC by Suharto Anggono
Modified: 2016-12-30 19:35 UTC (History)
1 user (show)

See Also:


Attachments
For 'rep', follow the longer or zero-length (633 bytes, patch)
2016-12-25 03:09 UTC, Suharto Anggono
Details | Diff
For 'rep', follow the longer or zero-length (1.14 KB, patch)
2016-12-25 06:04 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-12-25 03:01:38 UTC
On "Value", R 3.3.2 documentation "Extremes" says: "For 'pmin' or 'pmax', a vector of length the longest of the input vectors, or length zero if one of the inputs had zero length."

The "length zero" part doesn't hold if any of the inputs are classed objects.

Example:

R> pmax(I(character(0)), "")
[1] NA
R> pmax("", I(character(0)))
[1] ""

Compare with the following.

R> pmax(character(0), "")
character(0)
R> pmax("", character(0))
character(0)

R> sessionInfo()
R version 3.3.2 (2016-10-31)
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

loaded via a namespace (and not attached):
[1] tools_3.3.2


Because result of length zero is documented for vector inputs if any of them are of length zero, I think that similar behavior for classed object inputs would be better.
Comment 1 Suharto Anggono 2016-12-25 03:09:21 UTC
Created attachment 2199 [details]
For 'rep', follow the longer or zero-length
Comment 2 Suharto Anggono 2016-12-25 06:04:03 UTC
Created attachment 2200 [details]
For 'rep', follow the longer or zero-length
Comment 3 Martin Maechler 2016-12-29 08:22:07 UTC
(In reply to Suharto Anggono from comment #2)
> Created attachment 2200 [details]
> For 'rep', follow the longer or zero-length

seems correct to me.  Probably will commit the change (together with the other)
Comment 4 Martin Maechler 2016-12-30 16:00:30 UTC
(In reply to Martin Maechler from comment #3)
> (In reply to Suharto Anggono from comment #2)
> > Created attachment 2200 [details]
> > For 'rep', follow the longer or zero-length
> 
> seems correct to me.  Probably will commit the change (together with the
> other)


Indeed, it seems fine, and I have committed it with the other change,
in svn rev 71860, to R-devel.  
The plan is to port it to R-patched next week (if no problems surface).
Comment 5 Suharto Anggono 2016-12-30 16:50:21 UTC
From reg-tests-1d.R in R devel r71860, I am quite surprised by
identical(ob0, pmax(ob0, ""))
when 'ob0' is I(0[0]) .
I expect that pmax(I(0[0]), "") is I(character()) .

The cause can be seen in the following example.

mmm <- numeric(0)
mmm[logical(0)] <- character(0)
mmm  # numeric(0)
Comment 6 Suharto Anggono 2016-12-30 19:35:55 UTC
(In reply to Suharto Anggono from comment #5)
> mmm <- numeric(0)
> mmm[logical(0)] <- character(0)
> mmm  # numeric(0)

Interestingly, the following example is different. It gives the result that I expect.

mmm <- numeric(0)
mmm[logical(0)] <- ""
mmm  # character(0)