Error creating an alias function for .N

Open
#4,318 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the reproducible R example with data.table, focusing on the .N alias created through eval() and its interaction with mean(a) in the grouped expression. Trace the evaluation path for that combination and consider the shown subscript-out-of-bounds error the completion criterion; the issue does not name a source file or test.

Written by the indexing model from the issue text.

Description

programming

I am trying to create an alias function for .N, but run into an error that I think is caused by a data.table internal issue.

library(data.table)

test_df <- data.table(a = 1:3, b = c("a","a","b"))

get_n <- function() eval(quote(.N), parent.frame())

# Using get_n() alone works
test_df[, list(count = get_n()), by = b] # Works
#>    b count
#> 1: a     2
#> 2: b     1

# Using it with another summary function fails
test_df[, list(count = get_n(), avg_a = mean(a)), by = b]
#> Error in q[[2L]]: subscript out of bounds
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.