如何在 R 中对 DataFrame 进行排序?
在本文中,我们将讨论如何在 R 编程语言中对数据框进行排序。
对数据框进行排序的方法:
- order()函数(升序和降序)
- dplyr 包中的排列()函数
- data.table 包中的 setorder()函数
方法一:使用 order()函数
此函数用于根据数据帧中的特定列对数据帧进行排序
Syntax: order(dataframe$column_name,decreasing = TRUE))
where
- dataframe is the input dataframe
- Column name is the column in the dataframe such that dataframe is sorted based on this column
- Decreasing parameter specifies the type of sorting order
If it is TRUE dataframe is sorted in descending order. Otherwise, in increasing order
return type: Index positions of the elements
示例 1 :R 程序创建具有 2 列的数据框,并根据特定列按降序排列。以降序显示基于主题的排序数据框,以降序显示基于 rollno 的排序数据框
R
# create dataframe with roll no and
# subjects columns
data = data.frame(
rollno = c(1, 5, 4, 2, 3),
subjects = c("java", "python", "php", "sql", "c"))
print(data)
print("sort the data in decreasing order based on subjects ")
print(data[order(data$subjects, decreasing = TRUE), ] )
print("sort the data in decreasing order based on rollno ")
print(data[order(data$rollno, decreasing = TRUE), ] )
R
# create dataframe with roll no, names
# and subjects columns
data=data.frame(rollno = c(1, 5, 4, 2, 3),
names = c("sravan", "bobby",
"pinkey", "rohith",
"gnanesh"),
subjects = c("java", "python",
"php", "sql", "c"))
print(data)
print("sort the data in increasing order based on subjects")
print(data[order(data$subjects, decreasing = FALSE), ] )
print("sort the data in increasing order based on rollno")
print(data[order(data$rollno, decreasing = FALSE), ] )
print("sort the data in increasing order based on names")
print(data[order(data$names,decreasing = FALSE), ] )
R
# load the package
library("dplyr")
# create dataframe with roll no, names
# and subjects columns
data = data.frame(rollno = c(1, 5, 4, 2, 3),
names = c("sravan", "bobby", "pinkey",
"rohith", "gnanesh"),
subjects = c("java", "python", "php",
"sql", "c"))
# sort the data based on subjects
print(arrange(data, subjects))
R
# load the library
library("data.table")
# create dataframe with roll no, names
# and subjects columns
data=data.frame(rollno = c(1, 5, 4, 2, 3),
names = c("sravan", "bobby",
"pinkey", "rohith",
"gnanesh"),
subjects = c("java", "python",
"php", "sql", "c"))
# sort the data based on subjects
print(setorder(data,subjects))
输出:
rollno subjects
1 1 java
2 5 python
3 4 php
4 2 sql
5 3 c
[1] "sort the data in decreasing order based on subjects "
rollno subjects
4 2 sql
2 5 python
3 4 php
1 1 java
5 3 c
[1] "sort the data in decreasing order based on rollno "
rollno subjects
2 5 python
3 4 php
5 3 c
4 2 sql
1 1 java
示例 2: R 程序创建包含 3 列名为 rollno、names 和带有向量的主题的数据框,以递增顺序显示基于主题的 Sorted 数据框,以递增顺序显示基于 rollno 的 Sorted 数据框,显示基于的 Sorted 数据框名字按升序排列
电阻
# create dataframe with roll no, names
# and subjects columns
data=data.frame(rollno = c(1, 5, 4, 2, 3),
names = c("sravan", "bobby",
"pinkey", "rohith",
"gnanesh"),
subjects = c("java", "python",
"php", "sql", "c"))
print(data)
print("sort the data in increasing order based on subjects")
print(data[order(data$subjects, decreasing = FALSE), ] )
print("sort the data in increasing order based on rollno")
print(data[order(data$rollno, decreasing = FALSE), ] )
print("sort the data in increasing order based on names")
print(data[order(data$names,decreasing = FALSE), ] )
输出:
rollno names subjects
1 1 sravan java
2 5 bobby python
3 4 pinkey php
4 2 rohith sql
5 3 gnanesh c
[1] "sort the data in increasing order based on subjects"
rollno names subjects
5 3 gnanesh c
1 1 sravan java
3 4 pinkey php
2 5 bobby python
4 2 rohith sql
[1] "sort the data in increasing order based on rollno"
rollno names subjects
1 1 sravan java
4 2 rohith sql
5 3 gnanesh c
3 4 pinkey php
2 5 bobby python
[1] "sort the data in increasing order based on names"
rollno names subjects
2 5 bobby python
5 3 gnanesh c
3 4 pinkey php
4 2 rohith sql
1 1 sravan java
方法2:使用dplyr中的arrange()函数。
排列()用于按升序对数据框进行排序,它还会根据数据框中的列对数据框进行排序
Syntax: arrange(dataframe,column)
where
- dataframe is the dataframe input
- column is the column name , based on this column dataframe is sorted
我们需要安装 dplyr 包,因为它在该包中可用
Syntax: install.packages(“dplyr”)
示例:基于列对数据框进行排序的 R 程序
在这个程序中,我们使用向量创建了三列,并根据主题列对数据框进行排序
代码:
电阻
# load the package
library("dplyr")
# create dataframe with roll no, names
# and subjects columns
data = data.frame(rollno = c(1, 5, 4, 2, 3),
names = c("sravan", "bobby", "pinkey",
"rohith", "gnanesh"),
subjects = c("java", "python", "php",
"sql", "c"))
# sort the data based on subjects
print(arrange(data, subjects))
输出:
rollno names subjects
1 3 gnanesh c
2 1 sravan java
3 4 pinkey php
4 5 bobby python
5 2 rohith sql
方法 3:使用 data.table 包中的 setorder()
setorder用于以 set order 格式对数据帧进行排序。
Syntax: setorder(dataframe, column)
- Where dataframe is the input dataframe
- The column is the column name
示例:基于列对数据框进行排序的 R 程序
在这个程序中,我们使用向量创建了具有三列的数据框,并使用基于主题列的 setorder函数对数据框进行排序
代码:
电阻
# load the library
library("data.table")
# create dataframe with roll no, names
# and subjects columns
data=data.frame(rollno = c(1, 5, 4, 2, 3),
names = c("sravan", "bobby",
"pinkey", "rohith",
"gnanesh"),
subjects = c("java", "python",
"php", "sql", "c"))
# sort the data based on subjects
print(setorder(data,subjects))
输出:
rollno names subjects
5 3 gnanesh c
1 1 sravan java
3 4 pinkey php
2 5 bobby python
4 2 rohith sql