问下context by 怎么加到update!里面去

我想实现下面功能:

update factors set factor8 = factor8-dot(factors.factor2,factors.factor8)/dot(factors.factor2,factors.factor2)*factor2 from factors context by secID

写到

update!(factors, i, factors[s]-dot(factors[i],factors[s])/dot(factors[i],factors[i])*factors[i])

是这样,但缺context by。请教一下在DolphinDB database中怎么实现?

请先 登录 后评论

1 个回答

Juntao Wang

DolphinDB有个contextby函数(https://www.dolphindb.cn/cn/h... ),其语法为 contextby(func, funcArgs, groupingCol, [sortingCol]),可根据groupingCol分组,并在组内进行func(funcArgs)运算。
代码如下,其中factors[j]就是factor2:

factors[i] = contextby(def(x,y){return x - dot(x,y)/sum2(y)*y}, [factors[i], factors[j]], factors.secID)
请先 登录 后评论