如何在 R 中根据类别创建 ID 列?
如果我们在 R 数据框中有一个分类列,则它可以用来创建一个 ID 列,其中每个类别都将根据分类列中的类别定义其自己的 ID。
为此,我们需要使用 as.factor 和 as.numeric 函数读取分类列,如下面的示例所示。
示例 1
以下代码片段创建了一个示例数据框 -
Group<-sample(c("Male","Female"),20,replace=TRUE) Score<-sample(20:50,20) df1<-data.frame(Group,Score) df1
输出
创建了以下数据框 -
Group Score 1 Female 20 2 Female 27 3 Female 29 4 Male 50 5 Male 42 6 Female 41 7 Male 32 8 Male 25 9 Female 21 10 Female 49 11 Female 31 12 Female 28 13 Female 36 14 Female 26 15 Male 43 16 Female 45 17 Male 23 18 Female 46 19 Male 48 20 Male 33
要基于 df1 中的 Group 创建 ID 列,请将以下代码添加到上述代码片段中 -
Group<-sample(c("Male","Female"),20,replace=TRUE) Score<-sample(20:50,20) df1<-data.frame(Group,Score) df1$ID<-as.numeric(as.factor(df1$Group)) df1
输出
如果您将所有上述代码片段作为单个程序执行,它将生成以下输出 -
Group Score ID 1 Female 20 1 2 Female 27 1 3 Female 29 1 4 Male 50 2 5 Male 42 2 6 Female 41 1 7 Male 32 2 8 Male 25 2 9 Female 21 1 10 Female 49 1 11 Female 31 1 12 Female 28 1 13 Female 36 1 14 Female 26 1 15 Male 43 2 16 Female 45 1 17 Male 23 2 18 Female 46 1 19 Male 48 2 20 Male 33 2
示例 2
以下代码片段创建了一个示例数据框 -
Class<-sample(c("First","Second","Third"),20,replace=TRUE) Rank<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Rank) df2
输出
创建了以下数据框 -
Class Rank 1 Third 5 2 Third 7 3 First 3 4 Third 8 5 Second 9 6 Third 9 7 First 3 8 Second 10 9 First 4 10 Third 2 11 Third 8 12 Third 1 13 Third 10 14 First 6 15 Third 5 16 Second 6 17 Third 7 18 Third 5 19 Third 2 20 Second 5
要基于 df2 中的 Class 创建 ID 列,请将以下代码添加到上述代码片段中 -
Class<-sample(c("First","Second","Third"),20,replace=TRUE) Rank<-sample(1:10,20,replace=TRUE) df2<-data.frame(Class,Rank) df2$ID<-as.numeric(as.factor(df2$Class)) df2
输出
如果您将所有上述代码片段作为单个程序执行,它将生成以下输出 -
Class Rank ID 1 Third 5 3 2 Third 7 3 3 First 3 1 4 Third 8 3 5 Second 9 2 6 Third 9 3 7 First 3 1 8 Second 10 2 9 First 4 1 10 Third 2 3 11 Third 8 3 12 Third 1 3 13 Third 10 3 14 First 6 1 15 Third 5 3 16 Second 6 2 17 Third 7 3 18 Third 5 3 19 Third 2 3 20 Second 5 2
广告