Reshape data the first row becomes the different columns

2 Ansichten (letzte 30 Tage)
Luca
Luca am 23 Okt. 2023
Kommentiert: Luca am 23 Okt. 2023
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 23 Okt. 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
  2 Kommentare
Dyuman Joshi
Dyuman Joshi am 23 Okt. 2023
I didn't know about this functionality, neat!
Luca
Luca am 23 Okt. 2023
Thank you very much it worked.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by