Dmitrii I.
Dmitrii I.

Reputation: 726

Refering to the key of data.table in "by" argument not using key()

I would like to refer the key of a data table in the argument by without using function data.table::key.

For example, I can do this:

x <- data.table(mtcars, key='cyl')
x[, .(mean=mean(mpg)), by=key(x)]

cyl     mean
1:   4 26.66364
2:   6 19.74286
3:   8 15.10000

But I want to do this:

x <- data.table(mtcars, key='cyl')
x[, .(mean=mean(mpg)), by=.KEY]

cyl     mean
1:   4 26.66364
2:   6 19.74286
3:   8 15.10000

Now, .KEY does not exists, but is there an alias for they key of data table at all? I was not able to find it in the documentation of data.table nor by searching the web.

Referring to the key of x with .KEY (or its equivalent) does not require me to know the variable name x. I don't know why exactly, but it does not feel right or elegant to use key(x).

Upvotes: 1

Views: 84

Answers (1)

Dmitrii I.
Dmitrii I.

Reputation: 726

So it seems from the comments that there is no such way.

I have submitted a feature request: https://github.com/Rdatatable/data.table/issues/2130 for .KEY

Upvotes: 1

Related Questions