Unstack: how to create a pivot table like this?

3 views (last 30 days)
I have a table like (the real table is more complicated)
date month person spending
--------------------------
1/1/2020 January John 30
1/1/2020 January Mike 15
1/2/2020 January John 20
1/3/2020 January Kate 30
I want to create a pivot table like
month John's share of spending out of total spending | Mike's share | Kate's share
------------------------------------------
January
February
Please advise.
  1 Comment
alpedhuez
alpedhuez on 3 Jul 2020
first create a pivot table with spending of each and then calculate shares

Sign in to comment.

Accepted Answer

Monisha Nalluru
Monisha Nalluru on 7 Jul 2020
The following might be of help to you
  • Unstack function can be used to create the required model
  • The share is calculated as (amount/ total amount) *100 can be month or yearly
To display share for whole year , you can make use of this example
month = categorical({'Jan';'Jan';'Jan';'Jan'});
person = categorical({'John';'Mike';'John';'Kate'});
spendings = [30;15;20;30];
totalamount=sum(spendings);
share=[spendings/totalamount]*100;
T = table(month,person,share);
u = unstack(T,'share','person')
You can customize the data accordingly

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!