R语言排序三个基本函数:sort(),rank(),order()的用法
1、sort()
用法:sort(a,decreasing=T) 或 sort(a,decreasing=T)
其中,a为要排序的向量
# sort()排序,排序结果不可逆转
# 默认是升序
# decreasing为TRUE,表示降序
# decreasing为FALSE,表示升序
#排序后并不会修改原对象的值
#示例如下:
> a <- c(3,9,16,6,7,4,22,5,10,13)
> #sort()默认为从小到大(升序)排序,等同于decreasing=FALSE
> sort(a)
[1] 3 4 5 6 7 9 10 13 16 22
> sort(a,decreasing = F)
[1] 3 4 5 6 7 9 10 13 16 22
> #decreasing=TRUE,为从大到小(降序)排序
> sort(a,decreasing = T)
[1] 22 16 13 10 9 7 6 5 4 3
#排序并不会修改原对象的值,a仍为原来未排序的a
> a
[1] 3 9 16 6 7 4 22 5 10 13
2、order()
函数说明:返回的值表示位置,默认是升序,依次对应的是向量的最小值、次小值、第三小值…最大值
用法:order(a), a为要排序的向量
order(… = data, na.last = TRUE,decreasing = TRUE)
… 表示待排序向量
na.last 表示时候将NA值放在最后面&#x