Reshape data the first row becomes the different columns

2 次查看(过去 30 天)
Hi, I would like to reshape my data set, that includes different stocks i a year and is in the form of a table. Currently it look like this:
ID Date return
1 01.01.2015 0
1 02.01.2015 0.5
1 03.01.2015 -0.3
. . .
. . .
. . .
2 01.01.2015 0
2 02.01.2015 0.2
2 03.01.2015 0.5
. . .
. . .
. . .
The goal would be to have date colum as fist colum and every ID is a separate column. where every row shows returns for every stock in a specific day.
Date ID1 ID2 ID3 ...
01.01.2015 0 0 0
.
.
.
31.12.2015 0.2 0.1 0.5
Thank you for your help.

采纳的回答

Stephen23
Stephen23 2023-10-23
ID = [1;1;1;2;2;2];
Date = datetime(2015,1,[1;2;3;1;2;3]);
vals = [0;0.5;-0.3;0;0.2;0.5];
T = table(ID,Date,vals)
T = 6×3 table
ID Date vals __ ___________ ____ 1 01-Jan-2015 0 1 02-Jan-2015 0.5 1 03-Jan-2015 -0.3 2 01-Jan-2015 0 2 02-Jan-2015 0.2 2 03-Jan-2015 0.5
U = unstack(T,'vals','ID', 'VariableNamingRule','preserve')
U = 3×3 table
Date 1 2 ___________ ____ ___ 01-Jan-2015 0 0 02-Jan-2015 0.5 0.2 03-Jan-2015 -0.3 0.5

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by