组内的唯一ID [英] Unique ID within group
本文介绍了组内的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据集:
data <- data.table(match = c(1,1,1,1,2,2,2,2),
player = c("Dave", "Dave", "Dennis", "Dave", "Jake", "Jake", "Josh", "Jake"),
team = c("Australia", "Australia", "Australia", "Australia", "England", "England", "England", "England"))
我想创建一个ID变量,该变量指示给定比赛中团队中每个球员的位置.对于Give数据集,该变量将显示为:
I want to create an ID variable that indicates the position of each player in a team for a given match. The variable, for the give dataset, would look go:
ID = c(1,1,2,1,1,1,2,1)
我尝试使用group_by并在组内分配行号,但这不起作用.关于如何执行此操作的任何想法?
I have tried using group_by and assigning row number within a group but that isn't working. Any idea on how to do this?
推荐答案
我们可以使用 match
(该函数)
We can use match
(the function)
library(data.table)
data[, ID := match(player, unique(player)), match]
或使用 factor
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 中的
类似选项将是
Similar option in dplyr
would be
library(dplyr)
data %>%
group_by(match) %>%
mutate(ID = match(player, unique(player)))
这篇关于组内的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文