Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope of .N in i #1145

Closed
claytontstanley opened this issue May 11, 2015 · 7 comments
Closed

Scope of .N in i #1145

claytontstanley opened this issue May 11, 2015 · 7 comments
Assignees
Labels
Milestone

Comments

@claytontstanley
Copy link

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

@eantonya
Copy link
Contributor

Seems "obvious" to me that it should be equal to 2 in your second expression, instead of 5, so I'd vote yay for the change.

@jangorecki
Copy link
Member

+1 for change

@franknarf1
Copy link
Contributor

+1

1 similar comment
@renkun-ken
Copy link
Member

+1

@arunsrinivasan
Copy link
Member

Thanks @claytonstanley for catching this. Will fix.

@rsaporta
Copy link
Contributor

I like the change as well, but just to be sure all bases are covered, what about when toplevel .N is desired? (example that comes to mind might be when there is a by clause used in conjunction)


that being said, I do believe that the value of .N should match across i and j

@franknarf1
Copy link
Contributor

@rsaporta It's just a scalar, so I'd manually make a copy if needed: DT[,{g.N=.N;....SD...},by=g]

@arunsrinivasan arunsrinivasan added this to the v1.9.6 milestone May 28, 2015
@arunsrinivasan arunsrinivasan self-assigned this May 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants