How to write a function that calls a function that calls data.table?

前端 未结 2 1967
灰色年华
灰色年华 2020-12-08 16:10

The package data.table has some special syntax that requires one to use expressions as the i and j arguments.

This has some im

2条回答
  •  暖寄归人
    2020-12-08 16:34

    I think you might be tieing yourself up in knots. This works:

    library(data.table)
    foo <- function(data, by){
      by <- by
      data[, .N, by=by]
    }
    
    DT <- data.table(mtcars)
    foo(DT, 'gear')
    
    plotfoo <- function(data, by){
      foo(data, by)
    }
    plotfoo(DT, 'gear')
    

    And that method supports passing in character values:

    > gg <- 'gear'
    > plotfoo <- function(data, by){
    +   foo(data, by)
    + }
    > plotfoo(DT, gg)
       gear  N
    1:    4 12
    2:    3 15
    3:    5  5
    

提交回复
热议问题