Access `by` variable from within `j` in data.table

前端 未结 1 819
余生分开走
余生分开走 2021-02-14 05:01

If I have a data.table and I\'m doing some function call in the j, do I have access to the current value of the by variable?



        
相关标签:
1条回答
  • 2021-02-14 05:35

    Use .BY - which is a list of by variables:

    d <- data.table(x=1:10, y=c('a', 'b'))
    d[, .BY[[1]], by = y]  # [[1]] to access the first by variable, which is y
                           # if you had by = list(x, y) you'd do .BY[[2]] to access y
    #   y V1
    #1: a  a
    #2: b  b
    

    Additionally the list is named, so you can also access it by name:

    d[, .BY$y, by = y]
    
    0 讨论(0)
提交回复
热议问题