如何在R中使用range()?

Let’s learn to use range() in R returns the minimum and maximum values present in a vector or a data frame as well.

让我们学习在R中使用range()返回向量或数据帧中的最小值和最大值。

In this tutorial, we are going to see how the range() function works.

在本教程中,我们将了解range()函数的工作方式。

R中range()的基本语法 (Basic Syntax of range() in R)


range(x, na.rm=FALSE)

Where:

哪里:

  • x = vector or a data frame.

    x =向量或数据帧。
  • na.rm = removes the NA values if mentioned TURE and considers the NA values if mentioned FALSE.

    na.rm =如果提到TURE则删除NA值,如果提到FALSE则考虑NA值。


在R中使用range()的最小值和最大值 (Min and max values using range() in R)

R is an efficient language for data analysis. We have already discussed how to find the min and max values using functions min() and max() in R. In this tutorial, we are using range() function to find the minimum and maximum values.

R是用于数据分析的有效语言。 我们已经讨论了如何使用R中的min()和max()函数找到最小值和最大值。 在本教程中,我们使用range()函数查找最小值和最大值。

Finding the min and max values present in a vector or a data frame has numerous real-world usage. These values will be really helpful in the process of visualization and the analysis to get better accuracy in the result.

查找矢量数据帧中存在的最小值和最大值具有大量实际用途。 这些值在可视化和分析过程中将真正有帮助,以提高结果的准确性。



使用R中的range()在向量中查找最小值和最大值 (Finding Min and Max Values in a Vector using range() in R)

In this section, we are going to find the range of a vector. Let’s see how it works.

在本节中,我们将找到向量的范围。 让我们看看它是如何工作的。


#creates a vector of numerical values
df<- c(13,65,123,7,3,87,34,98)

#returns the range of the vector 
range(df)

Output = 3, 123

输出= 3,123

Note: In the output of the range() function, the first value represents the minimum values and the second value represents the maximum value present in the vector.

注意:range()函数的输出中,第一个值表示向量中存在的最小值,第二个值表示向量中存在的最大值。



查找具有NA值的向量的范围 (Finding the range of a vector having NA values)

Some times you have to deal with data having NA values. When the range() function encounters the NA values it returns NA instead of min and max values of the vector.

有时您必须处理具有NA值的数据。 当range()函数遇到NA值时,它将返回NA而不是向量的最小值和最大值。

You can see how range returns the NA values in the below output.

您可以在以下输出中看到range如何返回NA值。


#creates a vector having NA values
df<- c(45,76,89,NA,67,34,NA,56,78)

#returns the range of the vector
range(df)

Output = NA NA

输出= NA NA

Now to avoid this we use na.rm function which eliminates the NA values as shown below.

现在,为了避免这种情况,我们使用na.rm函数消除了如下所示的NA值。


#creates a vector with NA values
df<- c(45,76,89,NA,67,34,NA,56,78)

#returns the range of the vector by eliminating the NA values 
range(df,na.rm = T)

Output = 34 89

输出= 34 89



查找字符向量的范围 (Finding the range of a character vector )

Till now, we dealt with the range of numerical values. Now let’s find the range of a character vector.

到目前为止,我们已经处理了数值范围。 现在让我们找到一个字符向量的范围。

Let’s see how it works.

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


#creates a character vector
df<- c('Hayato','Antonio','Randrew','Jota','Adam','Commander')

#returns the range of the vector 
range(df)

Output = “Adam” “Randrew”

输出=“亚当”“兰德鲁”



查找数据框的范围 (Finding the range of a data frame)

In this section, let’s apply range() function to a data frame to get max and min values.

在本节中,让我们将range()函数应用于数据框以获取最大值和最小值。

For this purpose, let’s use ‘Quakes’ datasets which is provided by default in R studio.

为此,让我们使用R Studio默认提供的“ Quakes”数据集。

Quakes

Let’s find the range of the values present in the column ‘depth’.

让我们找到“ 深度”列中存在的值的范围


#returns the range of the values present in column 'depth'
range(quakes$depth)

Output = 40 680

输出= 40680

Range of the values present in the column ‘magnitude’.

“幅值”列中存在的值的范围。


#returns the range of the values present in the column 'mag'.
range(quakes$mag)

Output = 4.0 6.4

输出= 4.0 6.4



结语 (Wrapping up)

The min and max values in a data frame will be very helpful in the real-world data analysis. Using range() function, you can easily find the minimum and maximum values present in a vector or a data frame as well.

数据框中的最小值和最大值将对实际数据分析非常有帮助。 使用range()函数,您还可以轻松找到矢量或数据帧中的最小值和最大值。

We can also eliminate the NA values present in a vector or a data frame using na.rm function.

我们还可以使用na.rm函数消除矢量或数据帧中存在的NA值。

The min() and max() functions in R also capable of doing the same job as range() function. Both functions are very similar but effective.

R中的min()和max()函数还可以完成与range()函数相同的工作。 两种功能非常相似,但有效。

That’s all for now. Stay connected for more R programming articles. Happy learning!!!

目前为止就这样了。 保持联系以获取更多R编程文章。 学习愉快!

翻译自: https://www.journaldev.com/40367/range-in-r

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值