在 R 编程中获取向量的最小和最大元素 - range()函数
R 编程语言中的range()函数用于获取作为参数传递给它的向量的最小值和最大值。
Syntax: range(x, na.rm, finite)
Parameters:
- x: Numeric Vector
- na.rm: Boolean value to remove NA
- finite: Boolean value to exclude non-finite elements
R – range()函数示例
示例 1:R 语言中使用 range()函数获取向量的最小和最大元素
R
# R program to find the
# minimum and maximum element of a vector
# Creating a vector
x <- c(8, 2, 5, 4, 9, 6, 54, 18)
# Calling range() function
range(x)
R
# R program to find the
# minimum and maximum element of a vector
# Creating a vector
x <- c(8, 2, Inf, 5, 4, NA, 9, 54, 18)
# Calling range() function
range(x)
# Calling range() function
# excluding NA values
range(x, na.rm = TRUE)
# Calling range() function
# excluding finite values
range(x, na.rm = TRUE, finite = TRUE)
输出:
[1] 2 54
示例 2:在带有 NA 的 R 编程中使用 range()函数
R
# R program to find the
# minimum and maximum element of a vector
# Creating a vector
x <- c(8, 2, Inf, 5, 4, NA, 9, 54, 18)
# Calling range() function
range(x)
# Calling range() function
# excluding NA values
range(x, na.rm = TRUE)
# Calling range() function
# excluding finite values
range(x, na.rm = TRUE, finite = TRUE)
输出:
[1] NA NA
[1] 2 Inf
[1] 2 54