Skip to content

Scope of .N in i #1145

@claytontstanley

Description

@claytontstanley

I noticed that .N was made available in i recently, so I started using the feature. However, I got into trouble when trying to use .N in nested data tables. Here's an example:

require(data.table)
tbl = data.table(1:5)
tbl[1:.N]
#    V1
#1:  1
#2:  2
#3:  3
#4:  4
#5:  5

tbl[, head(.SD, n=2)[1:.N]]
#    V1
#1:  1
#2:  2
#3: NA
#4: NA
#5: NA

tbl[, head(.SD, n=2)[, 1:.N]]
# [1] 1 2

I was expecting .N in both i and j to bind to the most local data.table environment. In this case, the nested table, so .N=2.

I brought this issue up with Arun. He looked at the code and found that .N was locked to the top-level data.table. He thought this would be a good issue to bring to the community for discussion.

-Clayton

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions