I'm trying to have an anonymous function return multiple columns in the j
argument of a data.table
. Here's an example:
## sample data
tmpdt <- data.table(a = c(rep("a", 5), rep("b", 5)),
b = c(rep("f", 3), rep("r", 7)),
c = 1:10,
d = 21:30)
tmpdt[c %in% c(2,4), c := NA]
## this works fine
tmpdt[ , list(testout =
(function(x) {
model <- lm(c ~ d, x)
residuals(model)
})(.SD)),
by = a]
## but I want to return a data.frame from the
## anonymous function
tmpdt[ , list(testout =
(function(x) {
model <- lm(c ~ d, x)
tmpresid <- residuals(model)
tmpvalue <- x$b[as.numeric(names(tmpresid))]
data.frame(tmpvalue, tmpresid)
})(.SD)),
by = a]
The second version doesn't work because the function returns a data.frame
instead of just a vector. Is there any way to make this work without writing the function call outside of the data.table j
argument?
Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.
An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();
An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle . Anonymous functions can accept multiple inputs and return one output. They can contain only a single executable statement.
In Go, we can create a function without the function name, known as an anonymous function. For example, func () { fmt.Println("Function without name") } The above function is a valid function that prints "Function without name". It works just like a regular function in Go.
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
:
?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 for j
expressions?No anonymous function is passed to
j
. Instead, an anonymous body [{ }
] is passed toj
[...] Some programming languages call this a lambda.
{ }
in j
: Suppressing intermediate output with {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With