首页 > 解决方案 > 将名称转换为 R 中不同列中的标识码

问题描述

我是 R 新手,我正在努力解决以下问题:

我有一个或多或少这样的数据集:

NAME                     Collegue1                  Collegue 2
John Smith               Bill Gates                 Brad Pitt
Adam Sandler             Bill Gates                 John Smith
Bill Gates               Brad Pitt                  Adam Sandler
Brad Pitt                John Smith                 Bill Gates

我需要创建一个 ID 代码并用三列中的相应 ID 替换名称,我该怎么做?

标签: r

解决方案


也许你可以试试下面的代码

df[]<-as.integer(factor(unlist(df),levels = df$NAME))

这样

> df
  NAME Collegue1 Collegue2
1    1         3         4
2    2         3         1
3    3         4         2
4    4         1         3

或者

df[-1] <- as.integer(factor(unlist(df[-1]),levels = df$NAME))

这样

> df
          NAME Collegue1 Collegue2
1   John Smith         3         4
2 Adam Sandler         3         1
3   Bill Gates         4         2
4    Brad Pitt         1         3

数据

df <- structure(list(NAME = c("John Smith", "Adam Sandler", "Bill Gates", 
"Brad Pitt"), Collegue1 = c("Bill Gates", "Bill Gates", "Brad Pitt", 
"John Smith"), Collegue2 = c("Brad Pitt", "John Smith", "Adam Sandler", 
"Bill Gates")), class = "data.frame", row.names = c(NA, -4L))

推荐阅读