data.table:j 中的匿名函数 [英] data.table: anonymous function in j

查看:12
本文介绍了data.table:j 中的匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让匿名函数在 data.tablej 参数中返回多列.这是一个例子:

## 样本数据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]##这工作正常tmpdt[ , 列表(测试输出 =(函数(x){模型 <- lm(c ~ d, x)残差(模型)})(.SD)),由 = 一个]## 但我想从##匿名函数tmpdt[ , 列表(测试输出 =(函数(x){模型 <- lm(c ~ d, x)tmpresid <- 残差(模型)tmpvalue <- x$b[as.numeric(names(tmpresid))]data.frame(tmpvalue,tmpresid)})(.SD)),由 = 一个]

第二个版本不起作用,因为该函数返回一个 data.frame 而不仅仅是一个向量.有没有办法在 data.table j 参数之外编写函数调用来完成这项工作?

解决方案

你不需要匿名函数 - 你可以在 { } 中包含任何你想要的表达式(匿名 body) 在 j.

tmpdt[, {模型 <- lm(c ~ d, .SD)tmpresid <- 残差(模型)tmpvalue <- b[as.numeric(names(tmpresid))]list(tmpvalue, tmpresid) # 列表中的每个元素都成为结果中的一列}, 由 = 一个]


一些关于在j中使用匿名正文{ }的文档:

  1. ?data.tableExamples中的注释:

<块引用>

j 中的匿名 lambda:j 接受任何有效的表达式.记住:list 的每个元素都会成为结果中的一列.

  1. data.table FAQ 2.8 j 表达式的作用域规则是什么?

<块引用>

没有匿名函数被传递给j.相反,匿名 body [{ }] 被传递给 j [...] 一些编程语言称之为 lambda.

  1. Andrew Brooks 关于在 j 中使用 { } 的博文:使用 {}
  2. 抑制中间输出

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?

解决方案

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 {}

这篇关于data.table:j 中的匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆