data.table: anonymous function in j
You don't need an anonymous functions - you can have whatever expression you want wrapped in { }
(anonymous body) in j
.
tmpdt[, {
model <- lm(c ~ d, .SD)
tmpresid <- residuals(model)
tmpvalue <- b[as.numeric(names(tmpresid))]
list(tmpvalue, tmpresid) # every element of the list becomes a column in result
}
, by = a]
Some documentation on the use of anonymous body { }
in j
:
- Comment in Examples in
?data.table
:
anonymous lambda in
j
:j
accepts any valid expression. TO REMEMBER: every element of thelist
becomes a column in result.
data.table
FAQ 2.8 What are the scoping rules forj
expressions?
No anonymous function is passed to
j
. Instead, an anonymous body [{ }
] is passed toj
[...] Some programming languages call this a lambda.
- Blog post by Andrew Brooks on the use of
{ }
inj
: Suppressing intermediate output with {}