DolphinDB:如何按某一列对表进行分组并计算每组中列的相关性?

问题描述 投票:0回答:1

我想按“z”列对表格进行分组,并使用脚本

corr:R(table.x. table[y1y2...]
计算每组中“x”列与“y1”、“y2”列...的相关性。如何在 DolphinDB 中实现这一点?

group-by correlation dolphindb
1个回答
0
投票

您可以使用

groups
函数按“z”列对表进行分组,该函数返回一个元组,其元素是“z”列中每个唯一值的索引。使用脚本
t @ groups(t.z, 'tuple')
返回代表每组记录的表,并使用
corr
函数计算每组中“y1”、“y2”和“y3”列的相关性,如下脚本所示:

t = table(1..10 as x, 1 1 1 1 2 2 2 2 2 2 as z, rand(1.0, 10) as y1, rand(1.0, 10) as y2, rand(1.0,10) as y3)
each(f->corr:R(f.x, f[`y1`y2`y3]), t @ groups(t.z, 'tuple'))
© www.soinside.com 2019 - 2024. All rights reserved.