按键拆分R中的data.table
我在R中有一个按键拆分R中的data.table,r,split,key,data.table,grouping,R,Split,Key,Data.table,Grouping,我在R中有一个data.table对象,我想沿着它的键拆分它 >myTable[1:11] ID length hash 1: 2578 52.5 26566273 2: 4066 52.5 26566273 3: 2578 53.5 26566273 4: 4066 53.5 26566273 5: 2207 29.5 54352910 6: 3719 29.5 54352910 7: 5166 9.5 613353
data.table
对象,我想沿着它的键拆分它
>myTable[1:11]
ID length hash
1: 2578 52.5 26566273
2: 4066 52.5 26566273
3: 2578 53.5 26566273
4: 4066 53.5 26566273
5: 2207 29.5 54352910
6: 3719 29.5 54352910
7: 5166 9.5 613353882
8: 5167 9.5 613353882
9: 5169 9.5 613353882
10:5170 9.5 613353882
11:5171 9.5 613353882
首先,散列和长度是我的两个键
我想要的输出是ID列中每个键的列表
所以它可能是看起来像
first_hash length ID_list
26566273 52.5 [1] 2578 4066
26566273 53.5 [1] 2578 4066
54352910 29.5 [1] 2207 3719
613353882 9.5 [1] 5166 5167 5168 5169 5170 5171
或者某种列表
我认为plyr
可以给出一些答案,但我更喜欢data.table
方式
最终目标是创建具有相同密钥的所有ID对
我知道函数expand.grid
谢谢我们按“哈希”和“长度”分组,并将“ID”放在
列表中
DT <- myTable[,list(ID_list=list(ID)) , by =.(first_hash=hash, length)]
DT
# first_hash length ID_list
#1: 26566273 52.5 2578,4066
#2: 26566273 53.5 2578,4066
#3: 54352910 29.5 2207,3719
#4: 613353882 9.5 5166,5167,5169,5170,5171
str(DT)
# Classes ‘data.table’ and 'data.frame': 4 obs. of 3 variables:
# $ first_hash: int 26566273 26566273 54352910 613353882
# $ length : num 52.5 53.5 29.5 9.5
# $ ID_list :List of 4
# ..$ : int 2578 4066
# ..$ : int 2578 4066
# ..$ : int 2207 3719
# ..$ : int 5166 5167 5169 5170 5171
toString
是另一个常见的建议。很惊讶我找不到一个傻瓜。
myTable[,list(ID_list= toString(ID)) , by =.(first_hash=hash, length)]