Azzera filtri
Azzera filtri

Unstack: how to create a pivot table like this?

3 visualizzazioni (ultimi 30 giorni)
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 Commento
alpedhuez
alpedhuez il 3 Lug 2020
first create a pivot table with spending of each and then calculate shares

Accedi per commentare.

Risposta accettata

Monisha Nalluru
Monisha Nalluru il 7 Lug 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

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by