[Rd] which.max does not work on numeric_version class vectors
Ben Bolker
bbo|ker @end|ng |rom gm@||@com
Mon Dec 22 21:00:59 CET 2025
Sorry if this is obvious, but `which.max()` is hard-coded in C, with a
step at the beginning that attempts to coerce the vector to numeric
https://github.com/r-devel/r-svn/blob/5e0a5f013292e56c73360addb61ddaa7d3ca16a2/src/main/summary.c#L1027
Another alternative (your methods are probably better?)
which.max.nv <- function(x) (seq_along(x)[x == max(x)])[1]
It seems like too much work to change the definition of package versions
(which is a list of integer vectors, under the hood) or to make a generic
S3 method for which.max, given that the workarounds are so easy.
Also, the documentation explicitly calls out this limitation:
x: numeric (logical, integer or double) vector or an R object
for which the internal coercion to ‘double’ works whose ‘min’
or ‘max’ is searched for.
On Sat, Dec 20, 2025 at 10:35 AM Gabor Grothendieck <ggrothendieck using gmail.com>
wrote:
> versions <- c("9.10", "9.2")
> nv <- numeric_version(versions)
> class(nv)
> ## [1] "numeric_version"
>
> max(nv) # ok
> ## [1] ‘9.10’
>
> versions[tail(order(nv), 1)] # ok
> ## [1] "9.10"
>
> sort(nv, decreasing = TRUE)[1] # ok
> ## [1] ‘9.10’
>
> versions[which.max(xtfrm(nv))] # ok
> ## [1] "9.10"
>
> versions[which.max(nv)] # error
> ## Error in which.max(nv) : 'list' object cannot be coerced to type
> 'double'
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
[[alternative HTML version deleted]]
More information about the R-devel
mailing list