如何在 R 中使用最小值和最大值函数?
在本文中,我们将讨论 R 编程语言中的 Min 和 Max 函数。
分钟:
Min函数用于返回向量或数据帧中的最小值。
语法:
In a vector:
min(vector_name)
In a dataframe with in a column:
min(dataframe$column_name)
In a dataframe multiple columns:
min(c(dataframe$column1,dataframe$column2,......,dataframe$columnn))
In a whole dataframe across all columns
sapply(dataframe,min)
最大:
Max函数用于返回向量或数据框中的最大值。
语法:
In a vector:
max(vector_name)
In a dataframe with in a column:
max(dataframe$column_name)
In a dataframe multiple columns:
max(c(dataframe$column1,dataframe$column2,......,dataframe$columnn))
In a whole dataframe across all columns
sapply(dataframe,max)
示例 1:
此示例是一个 R 程序,用于获取向量中的最小值和最大值。
R
# create a vector
data = c(23, 4, 56, 21, 34, 56, 73)
# get the minimum value
print(min(data))
# get the maximum value
print(max(data))
R
# create a dataframe
data=data.frame(column1=c(23,4,56,21),
column2=c("sai","deepu","ram","govind"),
column3=c(1.3,4.6,7.8,6.3))
# get the minimum value in first column
print(min(data$column1))
# get the minimum value in second column
print(min(data$column2))
# get the minimum value in third column
print(min(data$column3))
# get the maximum value in first column
print(max(data$column1))
# get the maximum value in second column
print(max(data$column2))
# get the maximumvalue in third column
print(max(data$column3))
R
# create a dataframe
data = data.frame(column1=c(23, 4, 56, 21),
column2=c("sai", "deepu", "ram", "govind"),
column3=c(1.3, 4.6, 7.8, 6.3))
# get the minimum value across dataframe
print(sapply(data, min))
# get the maximum value across dataframe
print(sapply(data, max))
R
# create a dataframe
data=data.frame(column1=c(23,4,56,21),
column2=c("sai","deepu","ram","govind"),
column3=c(1.3,4.6,7.8,6.3))
# get the minimum value in multiple
# columns of dataframe
print(min(c(data$column1,data$column2,data$column3)))
# get the maximum value in multiple
# columns of dataframe
print(max(c(data$column1,data$column2,data$column3)))
输出:
[1] 4
[1] 73
示例 2:
此示例是一个 R 程序,用于获取数据框列中的最小值和最大值。
R
# create a dataframe
data=data.frame(column1=c(23,4,56,21),
column2=c("sai","deepu","ram","govind"),
column3=c(1.3,4.6,7.8,6.3))
# get the minimum value in first column
print(min(data$column1))
# get the minimum value in second column
print(min(data$column2))
# get the minimum value in third column
print(min(data$column3))
# get the maximum value in first column
print(max(data$column1))
# get the maximum value in second column
print(max(data$column2))
# get the maximumvalue in third column
print(max(data$column3))
输出:
[1] 4
[1] "deepu"
[1] 1.3
[1] 56
[1] "sai"
[1] 7.8
示例 3:
此示例是一个 R 程序,用于使用 fapply()函数获取数据帧中的最小值和最大值。
R
# create a dataframe
data = data.frame(column1=c(23, 4, 56, 21),
column2=c("sai", "deepu", "ram", "govind"),
column3=c(1.3, 4.6, 7.8, 6.3))
# get the minimum value across dataframe
print(sapply(data, min))
# get the maximum value across dataframe
print(sapply(data, max))
输出:
column1 column2 column3
"4" "deepu" "1.3"
column1 column2 column3
"56" "sai" "7.8"
示例 4:
此示例是一个 R 程序,用于获取数据框列之间的最大值和最小值。
R
# create a dataframe
data=data.frame(column1=c(23,4,56,21),
column2=c("sai","deepu","ram","govind"),
column3=c(1.3,4.6,7.8,6.3))
# get the minimum value in multiple
# columns of dataframe
print(min(c(data$column1,data$column2,data$column3)))
# get the maximum value in multiple
# columns of dataframe
print(max(c(data$column1,data$column2,data$column3)))
输出:
[1] "1.3"
[1] "sai"