R 编程中数据集的重新排序——arrange()函数
R语言中的arrange()
函数用于借助列名作为传递给函数的表达式对表行进行重新排序。
Syntax: arrange(x, expr)
Parameters:
x: data set to be reordered
expr: logical expression with column name
示例 1:
# R program to reorder a data set
# Loading library
library(dplyr)
# Calling dataset
x <- BOD
x
# Calling arrange() function
arrange(x, demand)
输出:
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
Time demand
1 1 8.3
2 2 10.3
3 5 15.6
4 4 16.0
5 3 19.0
6 7 19.8
示例 2:
# R program to reorder a data set
# Loading library
library(dplyr)
# Create a data frame
d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"),
age = c(7, 5, 9, 16) )
# Arranging name according to the age
d.name<- arrange(d, age)
print(d.name)
输出:
name age
1 Bhavesh 5
2 Abhi 7
3 Chaman 9
4 Dimri 16