How can one reshape this table?

2 views (last 30 days)
alpedhuez
alpedhuez on 30 Jul 2021
Edited: Matt J on 30 Jul 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?

Accepted Answer

Matt J
Matt J on 30 Jul 2021
Edited: Matt J on 30 Jul 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

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!