在data.table中有一种方法根据标识符按组分配ID? [英] Is there a way in data.table to assign ID's by group based upon an identifier?
问题描述
假设我有以下数据:
Name Date
Bank1 1/1/2005
Bank1 6/1/2005
Bank1 1/1/2006
Bank1 6/1/2006
Bank1 1/1/2007
Bank2 6/1/2006
Bank2 1/1/2007
Bank3 6/1/2005
Bank3 1/1/2006
Bank3 6/1/2006
Bank3 1/1/2007
....
这里的第一个日期是2005年1月1日,最后一个是1 / 1/2007。我想分配一个ID,以便我有:
Here the first date is 1/1/2005 and the last is 1/1/2007. I would like to assign an ID such that I have:
Name Date ID
Bank1 1/1/2005 1
Bank1 6/1/2005 2
Bank1 1/1/2006 3
Bank1 6/1/2006 4
Bank1 1/1/2007 5
Bank2 6/1/2006 4
Bank2 1/1/2007 5
Bank3 6/1/2005 2
Bank3 1/1/2006 3
Bank3 6/1/2006 4
Bank3 1/1/2007 5
....
以指定与其所有日期的顺序相对应的日期的ID。此外,Bank1具有所有日期的完整列表。我想知道如果有一个简单的方法在data.table做到这一点。我已经尝试过像循环,但在更高的维度它变得更难。任何建议将非常感谢,谢谢!
I basically want to assign an ID for the date corresponding to its order of all the dates. Furthermore, Bank1 has the complete list of all dates. I was wondering if there was a simple way in data.table to do this. I have tried things like loops but at higher dimensions it becomes harder. Any suggestions would be greatly appreciated, thanks!
推荐答案
我想你正在寻找特殊符号 .GRP
:
I think you're looking for the special symbol .GRP
:
dt[, id := .GRP, by=Date]
# Name Date id
# 1: Bank1 1/1/2005 1
# 2: Bank1 6/1/2005 2
# 3: Bank1 1/1/2006 3
# 4: Bank1 6/1/2006 4
# 5: Bank1 1/1/2007 5
# 6: Bank2 6/1/2006 4
# 7: Bank2 1/1/2007 5
# 8: Bank3 6/1/2005 2
# 9: Bank3 1/1/2006 3
# 10: Bank3 6/1/2006 4
# 11: Bank3 1/1/2007 5
这篇关于在data.table中有一种方法根据标识符按组分配ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!