Gabor Grothendieck
2025-Dec-20 15:34 UTC
[Rd] which.max does not work on numeric_version class vectors
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
Are objects of class 'numeric_version' of class 'numeric' ? I guess that is the problem... Best F. On 12/20/25 16:34, Gabor Grothendieck 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' > >[[alternative HTML version deleted]]
Ben Bolker
2025-Dec-22 20:00 UTC
[Rd] which.max does not work on numeric_version class vectors
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 at
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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
[[alternative HTML version deleted]]