diff --git a/src/library/base/R/version.R b/src/library/base/R/version.R index 5cb81212df..644a80964e 100644 --- a/src/library/base/R/version.R +++ b/src/library/base/R/version.R @@ -305,11 +305,11 @@ function(..., na.rm) y } else + # Subscripting with 1L ensures the result is NA with length 0 input. switch(.Generic, - max = x[which(v == max(v))[1L]], - min = x[which(v == min(v))[1L]], - range = x[c(which(v == min(v))[1L], - which(v == max(v))[1L])]) + max = x[which.max(v)[1L]], + min = x[which.min(v)[1L]], + range = x[c(which.min(v)[1L], which.max(v)[1L])]) } as.character.numeric_version <- diff --git a/tests/reg-tests-1e.R b/tests/reg-tests-1e.R index 0cb4f85e8f..d8c2c5dc58 100644 --- a/tests/reg-tests-1e.R +++ b/tests/reg-tests-1e.R @@ -483,6 +483,11 @@ stopifnot(identical(format(x[,2]), c(NA_character_, "0"))) is.na(x)[1] <- TRUE; stopifnot(identical(is.na(x), c(TRUE, FALSE))) ## gave two spurious warnings in R <= 4.2.2 +## NAs were not removed in R <= 4.5.2 (PR#19000) +x[3] <- "3.1" +stopifnot(identical(max(x, na.rm = TRUE), numeric_version("3.1"))) +stopifnot(identical(min(x, na.rm = TRUE), numeric_version("2.0"))) +stopifnot(identical(range(x, na.rm = TRUE), numeric_version(c("2.0", "3.1")))) mChk <- function(m) stopifnot(exprs = { identical(attributes(m), list(dim=2:3))