Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 组内的唯一ID_R_Dplyr - Fatal编程技术网

R 组内的唯一ID

R 组内的唯一ID,r,dplyr,R,Dplyr,我有以下数据集: 数据我们可以使用匹配(函数) 或使用因子 data[, ID := as.integer(factor(player, levels = unique(player))), match] data # match player team ID #1: 1 Dave Australia 1 #2: 1 Dave Australia 1 #3: 1 Dennis Australia 2 #4: 1 Dave Aust

我有以下数据集:



数据我们可以使用
匹配
(函数)

或使用
因子

data[, ID := as.integer(factor(player, levels = unique(player))), match]
data
#   match player      team ID
#1:     1   Dave Australia  1
#2:     1   Dave Australia  1
#3:     1 Dennis Australia  2
#4:     1   Dave Australia  1
#5:     2   Jake   England  1
#6:     2   Jake   England  1
#7:     2   Josh   England  2
#8:     2   Jake   England  1

dplyr
中的类似选项为

library(dplyr)
data %>%
   group_by(match) %>%
   mutate(ID = match(player, unique(player)))
library(dplyr)
data %>%
   group_by(match) %>%
   mutate(ID = match(player, unique(player)))