How can arrays be sorted in the R language?
In R language, you can use the sort() function to sort arrays. Here is an example code:
# 创建一个数组
arr <- c(8, 3, 5, 2, 9)
# 对数组进行排序
sorted_arr <- sort(arr)
# 输出排序后的数组
print(sorted_arr)
Running the code above will output the sorted array 2 3 5 8 9. In addition to the sort() function, you can also use the order() function to sort an array. Example code is shown below:
# 创建一个数组
arr <- c(8, 3, 5, 2, 9)
# 使用order函数对数组进行排序
sorted_arr <- arr[order(arr)]
# 输出排序后的数组
print(sorted_arr)
Running the code above will output the sorted array 2 3 5 8 9. Both methods are very convenient when sorting arrays.