Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table: anonymous function in j

Tags:

r

data.table

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?

like image 988
Rob Richmond Avatar asked Sep 17 '14 18:09

Rob Richmond


People also ask

What is an anonymous function in JS?

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.

What is anonymous function with example?

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();

Which function is called an anonymous function?

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.

What are anonymous functions in go?

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.


1 Answers

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:

  1. Comment in Examples in ?data.table:

anonymous lambda in j: j accepts any valid expression. TO REMEMBER: every element of the list becomes a column in result.

  1. 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 to j [...] Some programming languages call this a lambda.

  1. Blog post by Andrew Brooks on the use of { } in j: Suppressing intermediate output with {}
like image 123
eddi Avatar answered Oct 24 '22 02:10

eddi