在R中如何使用min()和max()函数

使用R中的min()和max()函数非常简单,可以轻松找到最小值和最大值。

你知道我们有像平均值、中位数、标准差和众数这样的函数来分别计算数值的平均、中间和离散程度。但你曾经想过有一个函数可以给出向量或数据框中的最小和最大值吗?

如果是这样的话,恭喜您,您已经有了名为min()和max()的函数,分别返回最小和最大值。

听起来很有趣对吗?让我们来看看它是如何工作的吧!


让我们从语法开始

min() 函数的语法如下所示。

min(x, na.rm = FALSE)
  • x = vector or a data frame.
  • na.rm = remove NA values, if it mentioned False it considers NA or if it mentioned True it removes NA from the vector or a data frame.

以下是max()函数的语法。

max(x, na.rm = FALSE)
  • x = vector or a data frame.
  • na.rm = remove NA values, if it mentioned False it considers NA or if it mentioned True it removes NA from the vector or a data frame.

在R中的Max()函数

在本节中,我们将找到向量中的最大值。为此,我们首先创建一个向量,然后应用max()函数,该函数返回向量中的最大值。

#creates a vector 
vector<-c(45.6,78.8,65.0,78.9,456.7,345.89,87.6,988.3)

#returns the max values present in the vector
max(vector)

输出= 988.3


R中的Min()函数可以得到一组数据的最小值。

在这里,我们将使用函数min()查找向量中的最小值。您可以创建一个向量,然后将min()应用于该向量,以返回最小值,如下所示。

#creates a vector
vector<-c(45.6,78.8,65.0,78.9,456.7,345.89,87.6,988.3)

#returns the minimum value present in the vector 
min(vector)

输出 = 45.6


在R中使用NA值的Max()函数。

在数据分析中,有时候你可能会在数据框或向量中遇到NA值。为了得到所需的结果,你需要绕过NA值。

如果max函数在过程中遇到NA值,将不会返回任何值。因此,您必须从向量或数据框中删除NA值才能获得最大值。

#creates a vector having NA values
df<- c(134,555,NA,567,876,543,NA,456)

#max function won't return any value because of the presence of NA. 
max(df)

#results in NA instead of max value
Output = NA

为了避免这个问题并获得最大值,我们使用na.rm函数从向量中移除NA值。现在你可以看到max()函数返回了最大值。

#max function with remove NA values is set as TRUE.
max(df, na.rm = T)

输出 = 876


在R中具有NA值的Min()函数

就像我们在上一节中应用了max函数一样,这里我们要找出一个含有NA值的向量中的最小值。

#creates a vector having NA values
df<- c(134,555,NA,567,876,543,NA,456)

#returns NA instead of minimum value
min(df)

输出 = 不适用

为了解决这个问题,我们使用na.rm函数从向量中删除NA值。现在你可以看到min()函数返回了最小值。

#creates a vector having NA values
df<- c(134,555,NA,567,876,543,NA,456)

#removes NA values and returns the minimum value in the vector
min(df, na.rm = T)

输出=134


一个字符向量中的Min()和Max()函数

到目前为止,我们已经处理了数值的最小值和最大值。如果我必须告诉你一些事情,我希望说的是你也可以找到字符向量的最小值和最大值。没错,你没听错!

让我们看看它是如何工作的!

对于字符向量的情况,最小值和最大值函数将考虑字母顺序,并相应地返回最小和最大值,如下所示。

#creates a character vector with some names
character_vector<- c('John','Angelina','Smuts','Garena','Lucifer')

#returns the max value
max(character_vector)

输出 = “烟斑”

同样地,我们可以使用 min() 函数来找到字符向量中的最小值,如下所示。

#creates a character vector with some names 
character_vector<- c('John','Angelina','Smuts','Garena','Lucifer')

#returns the minimum values in the vector
min(character_vector)

输出 = “安吉丽娜”


数据框中的Min()和Max()函数

让我们通过导入数据帧来寻找最小和最大值。数据集中的最小和最大值将对数据分布给出一个公正的概念。

这是在R Studio中可用的空气质量数据集。请注意,数据集中包含NA值。通过使用na.rm函数去除NA值的知识,让我们找出臭氧值的最小值和最大值。

Airquality Dataset
min(airquality$Ozone, na.rm=T)

输出 = 1

max(airquality$Ozone, na.rm = T)

输出=168

让我们找到空气质量数据集中温度值的最大值和最小值。

 min(airquality$Temp, na.rm = T)

输出 = 56

max(airquality$Temp, na.rm = T)

输出 = 97


总结

嗯,在这个教程中,我们重点关注了如何在向量、数据框和字符向量中找到最小值和最大值。

最小值和最大值函数可以在数值和字符串向量中使用。还可以使用na.rm函数去除NA值,以获得更好的准确性和所需的结果。

目前为止就只有这些了。希望你在上述章节中能找到更多的最小值和最大值。如果有任何问题,请务必在评论区提问。

开心学习!

更多学习:R文档

发表回复 0

Your email address will not be published. Required fields are marked *