Unstack: how to create a pivot table like this?

3 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 7 月 3 日
回答済み: Monisha Nalluru 2020 年 7 月 7 日
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 件のコメント
alpedhuez
alpedhuez 2020 年 7 月 3 日
first create a pivot table with spending of each and then calculate shares

サインインしてコメントする。

採用された回答

Monisha Nalluru
Monisha Nalluru 2020 年 7 月 7 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by