R语言排序函数

Python016

R语言排序函数,第1张

sort()是对向量进行从小到大的排序

rank()返回的是对向量中每个数值对应的秩

order()返回的值表示位置,依次对应的是向量的最小值、次小值、第三小值......最大值

rank() sort() order() 和 reorder()

data<=c(2,3,6,1)

reorder()函数在ggplot2中见过:

x = reorder(Var1, -Freq) Var1是分类变量,Freq是数值型变量

help(recorder) 的结果:

Reorder Levels of a Factor

Description

reorder is a generic function. The "default" method treats its first argument as a categorical variable, and reorders its levels based on the values of a second variable, usually numeric.

Usage

reorder(x, ...)

Default S3 method:

reorder(x, X, FUN = mean, ...,

order = is.ordered(x))

使用R包dplyr的函数arrange更简单,更简洁:

#多条件排序:使用dplyr::arrange

library(dplyr)

data("iris")

head(iris)

#第一列升序,然后是第三列升序

arrange(iris,iris[,1],iris[,3])

#第一列升序,然后是第三列降序

arrange(iris,iris[,1],-iris[,3])

扩展资料

R语言排序函数sort(),rank(),order()

>x<-c(97,93,85,74,32,100,99,67)

>sort(x)

[1]32677485939799100

>order(x)

[1]58432176

>rank(x)

[1]65431872

r语言怎么给多个表格加权重排序步骤如下。

1、函数是对一维度数组、向量x进行排序。若x为数值,则按照从小到大的原则进行排序。

2、数据分为确定值与缺失值两种。

3、最基本的排序,小数在前大数在后,相同元素先者在前后者在后。