How can one reshape this table?

6 visualizaciones (últimos 30 días)
alpedhuez
alpedhuez el 30 de Jul. de 2021
Editada: Matt J el 30 de Jul. de 2021
Consider a table T with
group date visitor
age 18-24 1/1/2021 10
age 18-24 1/2/2021 20
age 25-34 1/1/2021 10
age 25-34 1/2/2021 30
I want to reshape the matrix as
date age18-24 age 25-34
1/1/2021 10 10
1/2/2021 20 30
How can one do this?

Respuesta aceptada

Matt J
Matt J el 30 de Jul. de 2021
Editada: Matt J el 30 de Jul. de 2021
Perhaps as follows?
load tst
T
T = 4×3 table
group date visitor _____________ ____________ _______ {'age 18-24'} {'1/1/2021'} 10 {'age 18-24'} {'1/2/2021'} 20 {'age 25-34'} {'1/1/2021'} 10 {'age 25-34'} {'1/2/2021'} 30
[Gage,IDage]=findgroups(T(:,1));
[Gdate,IDdate]=findgroups(T(:,2));
Tnew=[ IDdate, array2table( accumarray([Gdate,Gage],T{:,3}) , 'Var',IDage{:,1})]
Tnew = 2×3 table
date age 18-24 age 25-34 ____________ _________ _________ {'1/1/2021'} 10 10 {'1/2/2021'} 20 30

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by