Borrar filtros
Borrar filtros

Reshape data the first row becomes the different columns

2 visualizaciones (últimos 30 días)
Luca
Luca el 23 de Oct. de 2023
Comentada: Luca el 23 de Oct. de 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.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by