如何将面板数据转为长格式?

huangapple go评论65阅读模式
英文:

How to unpivot panel data?

问题

我一直在使用矩阵格式的面板数据。现在,我想将该矩阵转换为窄数据,以便进行某些分析或处理任务。可以使用unpivot函数来实现这种转换吗?

英文:

I have been working with panel data in a matrix format. Now, I want to convert the matrix into narrow data for certain analysis or processing tasks. Can the unpivot function be used to achieve this transformation?

答案1

得分: 1

矩阵不能直接转换为窄表。要实现这种转换,您需要在应用unpivot函数之前将矩阵转换为表格。

//面板数据
n = 7
label = 2023.01.03 + 0..6
SH600000 = rand(4.0, n)//$DECIMAL64(3)
SH600004 = rand(14.0, n)//$DECIMAL64(3)
SH600006 = rand(114.0, n)//$DECIMAL64(3)
p = table(label, SH600000, SH600004, SH600006)
p = matrix(SH600000, SH600004, SH600006)
p.rename!(label, `SH600000`SH600004`SH600006)
t = table(p.rowNames() as label, p)
//解除枢轴
f = t.unpivot(`label, `SH600000`SH600006`SH600004, first)
select * from f order by label, valueType
英文:

A matrix cannot be directly converted to a narrow table. To achieve this transformation, you need to convert the matrix into a table before applying the unpivot function.

//panel data
n = 7
label = 2023.01.03 + 0..6
SH600000 = rand(4.0, n)//$DECIMAL64(3)
SH600004 = rand(14.0, n)//$DECIMAL64(3)
SH600006 = rand(114.0, n)//$DECIMAL64(3)
p = table(label, SH600000, SH600004, SH600006)
p = matrix(SH600000, SH600004, SH600006)
p.rename!(label, `SH600000`SH600004`SH600006)
t = table(p.rowNames() as label, p)
// unpivot
f = t.unpivot(`label, `SH600000`SH600006`SH600004, first)
select * from f order by label, valueType

huangapple
  • 本文由 发表于 2023年7月11日 14:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定