Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

data.table

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?

library(data.table)
d <- data.table(x=1:10, y=c('a', 'b'))

myfun <- function(DT) {
  print (DT$y)
}


d[, myfun(.SD), by=y]

For more context, I am passing a second argument (another data.table) to myfun and want to subset that based on the current value of y. It could be done with a dummy variable, but that seems yucky...

like image 904
Justin Avatar asked Feb 12 '14 16:02

Justin


1 Answers

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]
like image 160
eddi Avatar answered Oct 20 '22 06:10

eddi