如何在不考虑无限值的情况下计算 R 中的最大值和最小值?
问题描述:
我的数据具有以下示例的格式(此处为较小比例):
My data has the format of the example below (on a smaller scale here):
a <- c(NaN, Inf, 1, 2, 3, 4, 5)
我知道如果我在 R 中使用:
I know that if I use in R:
min(a, na.rm = TRUE)
或
max(a, na.rm = TRUE)
NaN
将被忽略而不是 Inf
,因此在 min(a, na.rm = TRUE)
中的结果是1
(因为 na.rm = TRUE
而忽略 NaN
),但结果在 max(a, na.rm =TRUE)
是 Inf
.
the NaN
will be disregarded but not the Inf
, so that in min(a, na.rm = TRUE)
the result is 1
(disregarding the NaN
because of na.rm = TRUE
), but the result in max(a, na.rm = TRUE)
is Inf
.
有没有办法在 R 中获得向量的最大值和最小值,同时忽略无穷大Inf
,以及忽略NaN
?
Is there any way to get, in R, the maximum and minimum of a vector and also disregard the infinite Inf
, as well as theNaN
is disregarded?
感谢您的帮助.
答
min(a[is.finite(a)])
# [1] 1
max(a[is.finite(a)])
# [1] 5