数据表“键索引”或“组计数器” [英] data.table "key indices" or "group counter"

查看:95
本文介绍了数据表“键索引”或“组计数器”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在data.table上创建密钥后:

After creating a key on a data.table:

set.seed(12345)
DT <- data.table(x = sample(LETTERS[1:3], 10, replace = TRUE),
                 y = sample(LETTERS[1:3], 10, replace = TRUE))
setkey(DT, x, y)
DT
#       x y
#  [1,] A B
#  [2,] A B
#  [3,] B B
#  [4,] B B
#  [5,] C A
#  [6,] C A
#  [7,] C A
#  [8,] C A
#  [9,] C C
# [10,] C C

得到一个整数向量,给每一行相应的键索引。我希望下面的预期输出( i )有助于澄清我的意思:

I would like to get an integer vector giving for each row the corresponding "key index". I hope the expected output (column i) below will help clarify what I mean:

#       x y i
#  [1,] A B 1
#  [2,] A B 1
#  [3,] B B 2
#  [4,] B B 2
#  [5,] C A 3
#  [6,] C A 3
#  [7,] C A 3
#  [8,] C A 3
#  [9,] C C 4
# [10,] C C 4

cumsum(!duplicate(DT [,key(DT),with = FALSE]))但希望有一个更好的解决方案。我觉得这个向量可能是表的内部表示的一部分,也许有一种方法来访问它?

I thought about using something like cumsum(!duplicated(DT[, key(DT), with = FALSE])) but am hoping there is a better solution. I feel this vector could be part of the table's internal representation, and maybe there is a way to access it? Even if it is not the case, what would you suggest?

推荐答案

更新:从 v1.8.3 :您可以直接使用内置的 .GRP

Update: From v1.8.3, you can simply use the inbuilt special .GRP:

DT[ , i := .GRP, by = key(DT)]

查看较早答案的历史记录。

See history for older answers.

这篇关于数据表“键索引”或“组计数器”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆