Skip to content

Commit 97f0caf

Browse files
author
maechler
committed
t.test(<Inf>,..) no longer errors unnecessarily
git-svn-id: https://svn.r-project.org/R/trunk@88283 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 2bd49a8 commit 97f0caf

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@
118118
such as \code{if(TRUE)} now longer triggers a new browser level, fixing
119119
\PR{18885} (differently than the fix for \PR{15770}), with thanks to
120120
\I{Ivan Krylov}.
121+
122+
\item \code{t.test(c(1:3, Inf))} and similar no longer produce an error but
123+
return a (still not so useful) \code{"htest"} result, fixing
124+
\PR{18901}, thanks to \I{Jesse Alderliesten}.
121125
}
122126
}
123127
}

src/library/stats/R/t.test.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/stats/R/t.test.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2019 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -60,7 +60,7 @@ function(x, y = NULL, alternative = c("two.sided", "less", "greater"),
6060
if(nx < 2) stop("not enough 'x' observations")
6161
df <- nx-1
6262
stderr <- sqrt(vx/nx)
63-
if(stderr < 10 *.Machine$double.eps * abs(mx))
63+
if(!is.na(stderr) && stderr < 10 *.Machine$double.eps * abs(mx))
6464
stop("data are essentially constant")
6565
tstat <- (mx-mu)/stderr
6666
method <- if(paired) "Paired t-test" else "One Sample t-test"
@@ -91,7 +91,7 @@ function(x, y = NULL, alternative = c("two.sided", "less", "greater"),
9191
stderr <- sqrt(stderrx^2 + stderry^2)
9292
df <- stderr^4/(stderrx^4/(nx-1) + stderry^4/(ny-1))
9393
}
94-
if(stderr < 10 *.Machine$double.eps * max(abs(mx), abs(my)))
94+
if(!is.na(stderr) && stderr < 10 *.Machine$double.eps * max(abs(mx), abs(my)))
9595
stop("data are essentially constant")
9696
tstat <- (mx - my - mu)/stderr
9797
}

src/library/stats/man/t.test.Rd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/stats/man/t.test.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2023 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{t.test}
@@ -69,6 +69,11 @@ t.test(x, \dots)
6969
7070
If the input data are effectively constant (compared to the larger of the
7171
two means) an error is generated.
72+
73+
If the data contain infinite values, \code{t.test()} no longer errors and
74+
returns a still not very useful result. Note that
75+
\code{\link{wilcox.test()}} is \emph{robust} against outliers and hence
76+
deals more usefully with such \code{Inf} values in \code{x} or \code{y}.
7277
}
7378
\value{
7479
A list with class \code{"htest"} containing the following components:

tests/reg-tests-1e.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,21 @@ stopifnot(exprs = {
19811981
## subsetting only kept "table" class in R <= 4.5.x
19821982

19831983

1984+
## t.test(<Inf>...) -- PR#18901
1985+
x <- c(1:6,Inf); y <- c(1:20, Inf); yN <- c(-Inf, 1:20)
1986+
(tt1 <- t.test(x))
1987+
tt2. <- t.test(x, y)
1988+
(tt2N <- t.test(x, yN))
1989+
stopifnot(exprs = {
1990+
inherits(tt1, "htest"); is.na(c(tt1 $p.value, tt1 $conf.int))
1991+
inherits(tt2., "htest"); is.na(c(tt2.$p.value, tt2.$conf.int))
1992+
inherits(tt2N, "htest"); is.na(c(tt2N$p.value, tt2N$conf.int))
1993+
tt1$estimate == Inf
1994+
tt2N$estimate == c(Inf, -Inf)
1995+
})
1996+
## The t.test() calls errored all in R <= 4.5.1
1997+
1998+
19841999

19852000
## keep at end
19862001
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)