在一个函数调用中将多个列添加到R data.table中? [英] Add multiple columns to R data.table in one function call?
问题描述
我有一个函数在列表中返回两个值。这两个值都需要添加到data.table中两个新列中。函数的评估是昂贵的,所以我想避免必须计算函数两次。例如:
library(data.table)
example(data.table)
DT
xyv
1:a 1 42
2:a 3 42
3:a 6 42
4:b 1 4
5:b 3 5
6:b 6 6
7:c 1 7
8:c 3 8
9:c 6 9
这是我的函数的一个例子。记住,我说这是昂贵的计算,除此之外,没有办法从其他给定值推断一个返回值(如下面的示例):
myfun < - function(y,v)
{
ret1 = y + v
ret2 = y - v
return ret1,r2 = ret2))
}
这是我在一个声明。然而,一个需要调用myfun两次:
DT [,new1:= myfun(y,v)$ r1] ,new2:= myfun(y,v)$ r2]
xyv new1 new2
1:a 1 42 43 -41
2:a 3 42 45 -39
3:a 6 42 48 -36
4:b 1 4 5 -3
5:b 3 5 8 -2
6:b 6 6 12 0
7: c 1 7 8 -6
8:c 3 8 11 -5
9:c 6 9 15 -3
有关如何执行此操作的任何建议?我可以在每次调用myfun时在单独的环境中保存 r2
,我只需要一次添加两列的引用。
您可以存储函数调用的输出:
z < - myfun(DT $ y,DT $ v)
head(DT [,new1:= z $ r1] [,new2:= z $ r2])
#xyv new1 new2
#[1,] a 1 42 43 -41
#[2,] a 3 42 45 -39
#[3,] a 6 42 48 -36
#[4,] b 1 4 5 -3
#[5,] b 3 5 8 -2
#[6,] b 6 6 12 0
,但这似乎也有效:
DT [,c(new1,new2):= myfun(y,v),with = FALSE]
新增于
data.table
v1.8.3在R-Forge,为方便起见,此处不再需要code> with = FALSE :,c(new1,new2):= myfun(y,v)]
到分钟现场新闻是这里。
I have a function that returns two values in a list. Both values need to be added to a data.table in two new columns. Evaluation of the function is costly, so I would like to avoid having to compute the function twice. Here's the example:
library(data.table) example(data.table) DT x y v 1: a 1 42 2: a 3 42 3: a 6 42 4: b 1 4 5: b 3 5 6: b 6 6 7: c 1 7 8: c 3 8 9: c 6 9
Here's an example of my function. Remember I said it's costly compute, on top of that there is no way to deduce one return value from the other given values (as in the example below):
myfun <- function (y, v) { ret1 = y + v ret2 = y - v return(list(r1 = ret1, r2 = ret2)) }
Here's my way to add two columns in one statement. That one needs to call myfun twice, however:
DT[,new1:=myfun(y,v)$r1][,new2:=myfun(y,v)$r2] x y v new1 new2 1: a 1 42 43 -41 2: a 3 42 45 -39 3: a 6 42 48 -36 4: b 1 4 5 -3 5: b 3 5 8 -2 6: b 6 6 12 0 7: c 1 7 8 -6 8: c 3 8 11 -5 9: c 6 9 15 -3
Any suggestions on how to do this? I could save
r2
in a separate environment each time I call myfun, I just need a way to add two columns by reference at a time.解决方案You could store the output of your function call:
z <- myfun(DT$y,DT$v) head(DT[,new1:=z$r1][,new2:=z$r2]) # x y v new1 new2 # [1,] a 1 42 43 -41 # [2,] a 3 42 45 -39 # [3,] a 6 42 48 -36 # [4,] b 1 4 5 -3 # [5,] b 3 5 8 -2 # [6,] b 6 6 12 0
but this also seems to work:
DT[, c("new1","new2") := myfun(y,v), with = FALSE]
New in
data.table
v1.8.3 on R-Forge, thewith = FALSE
is no longer needed here, for convenience :DT[, c("new1","new2") := myfun(y,v)]
Up to the minute live NEWS is here.
这篇关于在一个函数调用中将多个列添加到R data.table中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!