[英]R data.table apply function to sum of two columns
I have a data.table, and I'd like to apply a function over its columns. 我有一个data.table,我想在其列上应用一个函数。 Usually this is done like so:
通常这样做是这样的:
dt[, lapply(.SD, func), .SDcols = c("col1", "col2")]
And this would apply the function func
over those two columns. 这将在这两列上应用函数
func
。 What if, however, I'd like to apply it over the sum of those two columns? 但是,如果我想将它应用于这两列的总和呢? Something like
就像是
dt[, lapply(.SD, func), .SDcols = "col1 + col2"]
obviously doesn't work. 显然不起作用。
You could generalise this to applying func
to the result of another function (in this case, sum
) that takes in columns as arguments. 您可以将此概括为将
func
应用于另一个函数(在本例中为sum
)的结果,该函数将列作为参数。 I know I can create another column containing the results of the first function, but is there a way around that? 我知道我可以创建另一个包含第一个函数结果的列,但有没有办法解决这个问题呢?
To add the columns, try 要添加列,请尝试
dt[, func(Reduce(`+`,.SD)), .SDcols = c("col1","col2")]
This works with more than two columns as well, adding them all together before applying func
. 这也适用于两列以上,在应用
func
之前将它们全部添加在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.