R 编程中的数组排序
先决条件: R – 数组
向量是一维数组,由一维长度指定。可以使用“ c() ”函数创建 Vector。将值列表传递给c()函数以创建向量。可以按升序或降序进行排序。在排序之前应该记住一些事情。它们如下:
- 需要执行排序的顺序 - 升序/降序。
- 根据多列条件排序。
- 在排序过程中处理缺失值和重复值。分析师必须决定对缺失值和重复值应该做什么。在删除或替换空值之前,应考虑对数据的整体影响。
方法一:sort()函数
R 中的sort()函数用于对向量进行排序。默认情况下,它按升序对向量进行排序。要按降序排序,请向排序函数添加“递减”参数。
Syntax:
sort(name_of_vector, decreasing = TRUE)
Parameters:
name_of_vector: Vector to be sorted
decreasing: Boolean value to sort in descending order
示例 1:
R
# create a linear array
arr <- c(9, 8, 7, 6, 5, 4, 3, 2, 1)
# use of sort function to sort array
# by default it is sorted in increasing order
sort(arr)
R
# create linear array
arr <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
# use in built sort function
# to sort in decreasing order
sort(arr, decreasing = TRUE)
R
# define dataframe
df <- data.frame("Age" = c(12, 21, 15, 5, 25),
"Name" = c("Johnny", "Glen", "Alfie",
"Jack", "Finch"))
# sort the dataframe on the basis of
# age column and store it in newdf
newdf <- df[order(df$Age), ]
# print sorted dataframe
print(newdf)
R
# define vector
r = c(10, 20, 30, 40, 50, 60)
# sort in decreasing order
order(-r)
R
# define dataframe
df <- data.frame("Age" = c(12, 21, 15, 12, 25, 12),
"Name" = c("Johnny", "Glen", "Alfie",
"Jack", "Finch", "Aaron"))
# sort the dataframe first on the basis of
# Age and if age is same perform sort on Name
newdf <- df[order(df$Age, df$Name), ]
# print sorted dataframe
print(newdf)
R
# create linear array
arr <- c(9, 4, 5, 4, 5, 6, 3, 2, 1)
# repeat until break is encountered
repeat
{
# create a variable swap
swap = FALSE
# run loop from 2nd element till last element
for (i in 2:length(arr))
{
# copy original array into newArr
newArr <- arr
if (arr[i - 1] > arr[i])
{
newArr[i - 1] <- arr[i]
newArr[i] <- arr[i - 1]
arr <- newArr
swapped <- TRUE
}
}
if (!swapped) {break}
}
print(arr)
R
# install package dplyr
install.packages("dplyr")
# import library dplyr
library(dplyr)
# create dataframe
df <- data.frame("Age" = c(12, 21, 15, 5, 25),
"Name" = c("Johnny", "Glen", "Alfie",
"Jack", "Finch"))
# sort the dataframe on the basis of
# age column using arrange method
arrange(df,age)
输出:
[1] 1 2 3 4 5 6 7 8 9
示例 2:
电阻
# create linear array
arr <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
# use in built sort function
# to sort in decreasing order
sort(arr, decreasing = TRUE)
输出:
[1] 9 8 7 6 5 4 3 2 1
Note: The major drawback of the sort() function is that it cannot sort data frames.
方法二:order()函数
为了克服方法 1 的缺点,我们使用order()函数,该函数还根据指定的列对数据帧进行排序。要按降序排序,请添加负号。还可以使用多个条件对数据进行排序。假设如果两个人的年龄相同,那么我们可以根据他们的名字(即字典序)对他们进行排序。请参阅以下示例。
示例 1:
电阻
# define dataframe
df <- data.frame("Age" = c(12, 21, 15, 5, 25),
"Name" = c("Johnny", "Glen", "Alfie",
"Jack", "Finch"))
# sort the dataframe on the basis of
# age column and store it in newdf
newdf <- df[order(df$Age), ]
# print sorted dataframe
print(newdf)
输出:
Age Name
4 5 Jack
1 12 Johnny
3 15 Alfie
2 21 Glen
5 25 Finch
示例 2:
电阻
# define vector
r = c(10, 20, 30, 40, 50, 60)
# sort in decreasing order
order(-r)
输出:
[1] 6 5 4 3 2 1
示例 3:
电阻
# define dataframe
df <- data.frame("Age" = c(12, 21, 15, 12, 25, 12),
"Name" = c("Johnny", "Glen", "Alfie",
"Jack", "Finch", "Aaron"))
# sort the dataframe first on the basis of
# Age and if age is same perform sort on Name
newdf <- df[order(df$Age, df$Name), ]
# print sorted dataframe
print(newdf)
输出:
Age Name
6 12 Aaron
4 12 Jack
1 12 Johnny
3 15 Alfie
2 21 Glen
5 25 Finch
Note: The output above is the indices of numbers. For instance, 60 is the largest in vector and had index 6. Thus, 6 is displayed first.
方法 3:使用循环对数组进行排序
- 创建一个线性数组,比如 arr
- 创建一个变量交换。如果遍历整个数组后swap为false,则表示数组已经排序并中断循环
- 否则,运行循环并将原始数组复制到另一个数组中,例如 newArr,并开始比较原始数组中的相邻元素
- 如果当前元素小于前一个元素,则将arr的当前元素复制到newArr的前一个位置,将arr的前一个元素复制到newArr的当前位置。 newArr 现在已经交换了元素。
- 将 newArr 复制到原始数组 arr 并使 swap = TRUE。
- 重复直到 arr 排序
下面是上述方法的实现。
电阻
# create linear array
arr <- c(9, 4, 5, 4, 5, 6, 3, 2, 1)
# repeat until break is encountered
repeat
{
# create a variable swap
swap = FALSE
# run loop from 2nd element till last element
for (i in 2:length(arr))
{
# copy original array into newArr
newArr <- arr
if (arr[i - 1] > arr[i])
{
newArr[i - 1] <- arr[i]
newArr[i] <- arr[i - 1]
arr <- newArr
swapped <- TRUE
}
}
if (!swapped) {break}
}
print(arr)
输出:
[1] 1 2 3 4 4 5 5 6 9
方法四:使用dplyr包
dplyr 包易于使用且可靠。该包包括对数据进行排序的排列()方法。请参阅以下示例。
示例 1:
电阻
# install package dplyr
install.packages("dplyr")
# import library dplyr
library(dplyr)
# create dataframe
df <- data.frame("Age" = c(12, 21, 15, 5, 25),
"Name" = c("Johnny", "Glen", "Alfie",
"Jack", "Finch"))
# sort the dataframe on the basis of
# age column using arrange method
arrange(df,age)
输出:
Age Name
4 5 Jack
1 12 Johnny
3 15 Alfie
2 21 Glen
5 25 Finch