r语言中的order函数
The order() function in R is very useful in sorting a particular data values according to a specific variable.
R中的order()函数在根据特定变量对特定数据值进行排序时非常有用。
Hello folks, changing the order of the values or elements present in a data frame or a vector is found to be very effective in data analysis. Hence, this R tutorial is focussed on the order() function which is used to order the data.
大家好,更改数据框或向量中存在的值或元素的顺序在数据分析中非常有效。 因此,本R教程侧重于用于对数据进行排序的order()函数。
Order() = The order() function will arranges the data or orders the data based on given parameters.
Order()= order()函数将根据给定的参数排列数据或对数据进行排序。
order(x,decreasing=F,na.last=NA)
Where,
哪里,
x =数据帧或向量
reduction =以递减的方式对数据进行排序,默认情况下为FALSE。
na.last =将NA元素移到最后,否则,也将NA放在第一个。
In this section, we can try to order a simple vector having some values in it. Let’s make use of order() function to order the values as well.
在本节中,我们可以尝试订购其中包含某些值的简单向量。 让我们也使用order()函数对值进行排序。
This is a simple vector and now we can try to order the values.
这是一个简单的向量,现在我们可以尝试对值进行排序。
#creates a vector
x<-c(3.5,7.8,5.6,1.1,2.9,4.4)
#orders the values in the vector in an ascending or increasing fashion
x[order(x)]
You will get the values arranged in the increasing fashion because by default the parameter is set as decreasing = F.
您将获得以递增方式排列的值,因为默认情况下该参数设置为减少= F。
Output = 1.1 2.9 3.5 4.4 5.6 7.8
输出= 1.1 2.9 3.5 4.4 5.6 7.8
The above output shows the ascending order, now let’s order the values in a decreasing fashion.
上面的输出显示了升序,现在让我们以递减的方式对值进行排序。
#creates a vector
x<-c(3.5,7.8,5.6,1.1,2.9,4.4)
#orders the data in the decreasing fashion
x[order(x,decreasing = T)]
Output = 7.8 5.6 4.4 3.5 2.9 1.1
输出= 7.8 5.6 4.4 3.5 2.9 1.1
With the help of order() function, you can easily order the values in both increasing and decreasing fashion as well. Let’s see how it works.
借助order()函数,您还可以轻松地对值进行递增和递减排序。 让我们看看它是如何工作的。
Let’s create a data frame using ‘tibble’ function.
让我们使用“ tibble”功能创建一个数据框。
Tibble: Tibble in R is widely used to generate a data frame with random values. It may include multiple rows and columns as well.
Tibble: R中的Tibble被广泛用于生成具有随机值的数据帧。 它也可以包括多个行和列。
#import the required package
library(dplyr)
#creates a data frame with normal distribution values.
df<-tibble(
column1=rnorm(5,5,1.5),
column2=rnorm(5,5,1.5),
column3=rnorm(5,5,1.5),
column4=rnorm(5,5,1.5))
The data frame created using tibble function is shown below.
使用tibble功能创建的数据帧如下所示。
Let’s order the particular columns in this data frame.
让我们对这个数据框中的特定列进行排序。
#orders the values in the column 1
df[order(df$column1),]
As you can in the above output, column 1 has been sorted in increasing fashion.
正如您在上面的输出中可以看到的那样,列1的排序方式是递增的。
Let’s see how we can order the column in a data frame as well and also using the ‘-‘ (negative) sign to order the values in a decreasing fashion.
让我们看看如何在数据框中对列进行排序,以及如何使用“-” (负号)对值进行递减排序。
#orders the values [decresing the column values]
df[order(-df$column1),]
As you can see in the above piece of code column 1 is backed by the negative ‘-‘ sign which indicates the decreasing order. Like this method also you can change the ordering fashion instead of mentioning parameters as shown in the syntax and in the above examples.
如您在上面的代码段中所见,第1列由负的“-”号表示 ,该负号表示降序 。 像这种方法一样,您也可以更改排序方式,而不必像语法和上面的示例中所示提到参数。
It will be exciting to order a matrix, isn’t it? Well, in this section let’s try to create a matrix and after that, we are going to order that matrix using order() function as well.
订购矩阵会令人兴奋,不是吗? 好了,在本节中,我们尝试创建一个矩阵 ,然后,我们还将使用order()函数对该矩阵进行排序。
Let’s see how it works.
让我们看看它是如何工作的。
#creates a 5x5 matrix having values from 1 to 25
x<-matrix(1:25,5,5)
x
We have created a 5×5 matrix, where the values present in the matrix will be 1:25 i.e. numbers from 1 to 25. Now we are all set to order the matrix.
我们创建了一个5×5矩阵,其中矩阵中的值将为1:25,即1到25之间的数字。现在我们都已设置好对矩阵进行排序。
#orders the column1 in the matrix
x[order(x[,1],decreasing = T),]
In the above output, you can see that the values present in the matrix were ordered in the decreasing fashion. You can also reorder the matrix in an ascending fashion by mentioning decreasing = F in the above code.
在上面的输出中,您可以看到矩阵中存在的值以递减的方式排序。 您还可以通过在上面的代码中提及reduction = F来以升序方式对矩阵重新排序。
Till now, we have discussed many examples. Now, as a final step, we are going to order a data frame as it is our final motive. Because finally all of us have to deal with a data set. So working or practicing on a data set will be really helpful for all of us.
到目前为止,我们已经讨论了许多示例。 现在,作为最后一步,我们将订购数据框,因为这是我们的最终动机。 因为最后我们所有人都必须处理数据集。 因此,对数据集进行工作或练习对我们所有人都将真正有帮助。
Let’s see how it works.
让我们看看它是如何工作的。
First, we are going to import a dataset, where we are applying all our discussed techniques and topics as well.
首先,我们将导入一个数据集,其中还将应用所有讨论的技术和主题。
The below is the data of the ‘airquality’ dataset which is usually available by default in R.
以下是“空气质量”数据集的数据,通常默认情况下在R中可用。
I particularly choose ‘airquality’ dataset, because it includes NA values as well. So let’s apply all our findings in the above sections into this data set and order the values.
我特别选择“空气质量”数据集,因为它也包含NA值。 因此,让我们将以上各节中的所有发现应用到该数据集中并对值进行排序。
We are operating on the ‘Ozone’ column values in the data set.
我们正在对数据集中的“臭氧”列值进行操作。
#orders the values by eliminating NA values in Ozone column
x[order(x$Ozone,decreasing = F,na.last = NA),]
As you can see in the above output, all the NA values will be excluded by the na.last = NA command. You can also observe that the values are arranged in increasing fashion as well.
如您在上面的输出中看到的, na.last = NA命令将排除所有NA值。 您还可以观察到值也以递增方式排列。
Now, let’s order the values in the decreasing fashion.
现在,让我们以递减的方式对值进行排序。
x[order(x$Ozone,decreasing = T,na.last = NA),]
In the above output, you can see that the values are arranged in the decreasing fashion with the elimination of the NA values in the Ozone column
在上面的输出中,您可以看到在消除臭氧列中的NA值后,这些值以递减的方式排列
Well, in this tutorial we have discussed the order() function and its uses and samples in R. I hope by now you understood the topic well and implement in your analysis as well.
好吧,在本教程中,我们讨论了order()函数及其在R中的用法和示例。我希望到目前为止,您已经很好地理解了该主题并在您的分析中实现了它。
That’s all about the order() function in R. For more updates, stay tuned. For any queries or doubts, don’t hesitate to hit the comment section. Happy ordering!!!
这就是R中的order()函数。有关更多更新,请继续关注。 如有任何疑问或疑问,请随时点击评论部分。 订购愉快!
More study: R documentation
更多研究: R文档
翻译自: https://www.journaldev.com/40675/order-function-in-r
r语言中的order函数