How to create a pivot table

조회 수: 45 (최근 30일)
alpedhuez
alpedhuez 2020년 6월 29일
답변: Lola Davidson 2023년 3월 17일
I have a table
date person spendings
----------------------------------------
1/1/2020 John $100
1/1/2020 Mike $50
1/2/2020 John $75
1/3/2020 Mike $40
I want to create a pivot table
John Spendings Mike Spendings
----------------------------------------------------------------------------------
January 2020
February 2020
March 2020
Please advise.

채택된 답변

Maadhav Akula
Maadhav Akula 2020년 7월 2일
I think the unstack function might help you out. I have written a small example using your data:
month = categorical({'Jan';'Jan';'Feb';'March'});
person = categorical({'John';'Mike';'John';'Mike'});
spendings = [100;50;75;40];
T = table(month,person,spendings)
u = unstack(T,'spendings','person')
I think you can customise it according to your needs.
Hope this helps!
  댓글 수: 3
Maadhav Akula
Maadhav Akula 2020년 7월 2일
You can utilize the month and datetime functions to tweak them to your required format.
a = month(datetime('1/1/2020','InputFormat','dd/MM/yyyy'),'shortname')
The above line returns the Month in 'Mmm' format, for instance January as 'Jan'. You can tweak some of these properties to obtain the required values which best suits your requirements.
alpedhuez
alpedhuez 2020년 7월 3일
편집: alpedhuez 2020년 7월 3일
date=datetime(T2.yyyy_mm,'InputFormat','yyyy-MM');

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Lola Davidson
Lola Davidson 2023년 3월 17일
As of R2023a, you can use the "pivot" function: https://www.mathworks.com/help/matlab/ref/pivot.html
Tout = pivot(Tin, Rows="date", RowsBinMethod="month", Columns="person", DataVariable="spendings");

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by