Skip to content

Error creating an alias function for .N #4318

@markfairbanks

Description

@markfairbanks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    programmingparameterizing queries: get, mget, eval, env

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions