创建等级变量以将数字变量转换为序数变量。这对于非参数分析很有用,因为如果数值变量的分布不正常或存在参数分析的假设,数值变量不能遵循这些假设,那么原始变量值将不被直接分析。要使用mutate函数创建等级变量,我们可以使用density_rank参数。
请看以下数据帧-
set.seed(7) x1 <-sample(1:5,20,replace=TRUE) x2 <-rep(c("Winter","Spring","Summer","Rainy"),times=5) df <-data.frame(x1,x2) df
输出结果
x1 x2 1 2 Winter 2 3 Spring 3 4 Summer 4 2 Rainy 5 2 Winter 6 3 Spring 7 3 Summer 8 2 Rainy 9 4 Winter 10 3 Spring 11 4 Summer 12 2 Rainy 13 3 Winter 14 5 Spring 15 4 Summer 16 3 Rainy 17 2 Winter 18 2 Spring 19 4 Summer 20 3 Rainy library(dplyr)
为x1变量创建等级变量-
df%>%mutate(Rank_x1=dense_rank(desc(-x1)))
输出结果
x1 x2 Rank_x1 1 2 Winter 1 2 3 Spring 2 3 4 Summer 3 4 2 Rainy 1 5 2 Winter 1 6 3 Spring 2 7 3 Summer 2 8 2 Rainy 1 9 4 Winter 3 10 3 Spring 2 11 4 Summer 3 12 2 Rainy 1 13 3 Winter 2 14 5 Spring 4 15 4 Summer 3 16 3 Rainy 2 17 2 Winter 1 18 2 Spring 1 19 4 Summer 3 20 3 Rainy 2
让我们看另一个例子-
grp <-rep(c(28,29,31,45,37),times=4) Percentage <-rep(c(28,29,31,45,37),times=4) ID <-1:20 df_new <-data.frame(ID,Percentage) df_new
输出结果
ID Percentage 1 1 28 2 2 29 3 3 31 4 4 45 5 5 37 6 6 28 7 7 29 8 8 31 9 9 45 10 10 37 11 11 28 12 12 29 13 13 31 14 14 45 15 15 37 16 16 28 17 17 29 18 18 31 19 19 45 20 20 37
df%>%mutate(Rank_Percentage=dense_rank(desc(-Percentage)))
输出结果
x1 x2 Rank_Percentage 1 2 Winter 1 2 3 Spring 2 3 4 Summer 3 4 2 Rainy 5 5 2 Winter 4 6 3 Spring 1 7 3 Summer 2 8 2 Rainy 3 9 4 Winter 5 10 3 Spring 4 11 4 Summer 1 12 2 Rainy 2 13 3 Winter 3 14 5 Spring 5 15 4 Summer 4 16 3 Rainy 1 17 2 Winter 2 18 2 Spring 3 19 4 Summer 5 20 3 Rainy 4