Effacer les filtres
Effacer les filtres

Reshape data the first row becomes the different columns

1 vue (au cours des 30 derniers jours)
Luca
Luca le 23 Oct 2023
Commenté : Luca le 23 Oct 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.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by