Clear Filters
Clear Filters

Reshape data the first row becomes the different columns

1 view (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 23 Oct 2023
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

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!