gpt4 book ai didi

r - 如何在 data.table 的分组依据中获取组名?

转载 作者:行者123 更新时间:2023-12-02 08:02:40 25 4
gpt4 key购买 nike

我想将一个函数应用到 data.table 的列,但我想考虑应用该函数的组,即我想将组值作为参数传递给该函数。但是,在应用我的函数时,我无法获取组名。

如何获取组值?或者我应该使用不同的方法吗?

例子:

library(data.table)
set.seed(369)
dta <- data.table(gr = 1:5,
a = rnorm(5),
b = rnorm(5),
c = rnorm(5),
d = rnorm(5))

add <- function(x, y, group){
if(group == 1){
x + y
} else{
x - y
}
}

dta[, newcol := add(c, d), by = (gr)]

我不知道如何将当前组的值传递给函数

最佳答案

Note1: My quick draw answer erroneously suggested using .GRP, which happens to return the same answer in this specific example. Per @MichaelChirico 's recommendation, .BY is the proper special symbol to use.

Note 2: Thanks @Frank for the additional feedback -- I created a quick gist here of some experimentation with .BY, and have updated the answer again to properly reflect the need to reference the grouping columns in the list by name.

看起来特殊符号 .BY 正是您所追求的。要了解有关 .BY 和其他符号如何工作的更多信息,请在控制台中运行 help("special-symbols") 以查看文档。

library(data.table)

set.seed(369)
dta <- data.table(gr = 1:5,
a = rnorm(5),
b = rnorm(5),
c = rnorm(5),
d = rnorm(5))

add <- function(x, y, group){
if(group == 1){
x + y
} else{
x - y
}
}

dta[, newcol := add(c, d, .BY$gr), by = (gr)]

print(dta)

# gr a b c d newcol
# 1: 1 -0.7506434 1.08042639 -0.57234502 -0.009598695 -0.5819437
# 2: 2 0.8976528 -0.45909601 -0.08179559 -1.359655922 1.2778603
# 3: 3 0.7449628 -0.92638505 -1.11577747 0.654088229 -1.7698657
# 4: 4 0.5811869 -0.07451776 -0.50771981 -1.009298251 0.5015784
# 5: 5 -0.3270194 0.97218850 0.55705663 -0.032128474 0.5891851

Note 3: This also works just fine for most use cases and might be a little bit more intuitive:

dta[, newcol := add(c, d, gr), by = (gr)]

关于r - 如何在 data.table 的分组依据中获取组名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55166181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com