在 R 编程中获取对象的最大元素 - max()函数
R 语言中的max()
函数用于查找对象中存在的最大元素。这个对象可以是一个向量、一个列表、一个矩阵、一个数据框等。
Syntax: max(object, na.rm)
Parameters:
object: Vector, matrix, list, data frame, etc.
na.rm: Boolean value to remove NA element.
示例 1:
# R program to illustrate
# the use of max() function
# Creating vectors
x1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
x2 <- c(1, 4, 2, 8, NA, 11)
# Finding Maximum element
max(x1)
max(x2, na.rm = FALSE)
max(x2, na.rm = TRUE)
输出:
[1] 9
[1] NA
[1] 11
示例 2:
# R program to illustrate
# the use of max() function
# Creating a matrix
arr = array(2:13, dim = c(2, 3, 2))
print(arr)
# Using max() function
max(arr)
输出:
,, 1
[, 1] [, 2] [, 3]
[1, ] 2 4 6
[2, ] 3 5 7,, 2
[, 1] [, 2] [, 3]
[1, ] 8 10 12
[2, ] 9 11 13
[1] 13