R sort() Function: Descending Order

In R language, we can use the sort() function to sort vectors or data frames in descending order. Here is a simple example:

# 创建一个向量
x <- c(10, 5, 8, 3, 2)

# 对向量进行从大到小的排序
sorted_x <- sort(x, decreasing = TRUE)

# 打印排序后的向量
print(sorted_x)

After running the above code, the vector will be output in descending order.

[1] 10  8  5  3  2

To sort a column in a data frame, you can use the following sample code:

# 创建一个数据框
df <- data.frame(x = c(10, 5, 8, 3, 2), y = c("a", "b", "c", "d", "e"))

# 对数据框的x列进行从大到小的排序
sorted_df <- df[order(-df$x), ]

# 打印排序后的数据框
print(sorted_df)

After running the above code, the output will display the result of sorting the data frame in descending order based on the x column.

bannerAds