data.table:j 中的匿名函数 [英] data.table: anonymous function in j
问题描述
我试图让匿名函数在 data.table
的 j
参数中返回多列.这是一个例子:
## 样本数据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
中使用匿名正文{ }
的文档:
?data.table
中Examples中的注释:
<块引用>
j
中的匿名 lambda:j
接受任何有效的表达式.记住:list
的每个元素都会成为结果中的一列.
<块引用>
没有匿名函数被传递给j
.相反,匿名 body [{ }
] 被传递给 j
[...] 一些编程语言称之为 lambda.
- Andrew Brooks 关于在
j
中使用{ }
的博文:使用 {} 抑制中间输出
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
:
- 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.
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 {}
这篇关于data.table:j 中的匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!