Unstack: how to create a pivot table like this?

3 Ansichten (letzte 30 Tage)
alpedhuez
alpedhuez am 3 Jul. 2020
Beantwortet: Monisha Nalluru am 7 Jul. 2020
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 Kommentar
alpedhuez
alpedhuez am 3 Jul. 2020
first create a pivot table with spending of each and then calculate shares

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Monisha Nalluru
Monisha Nalluru am 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

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by