英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论