how to user grpstats function in this case

3 views (last 30 days)
I have a table like (the real table is more complicated)
date month person spending
1/1/2020 January-2020 John 30
1/1/2020 January-2020 Mike 15
1/2/2020 January-2020 John 20
1/3/2020 January-2020 Kate 30
then I want to create a summary table such as
person 'count of John's spending data' 'sum of John's spending' 'average of John's spending per observation'
January-2020
February-2020
Please advise.

Accepted Answer

dpb
dpb on 3 Jul 2020
>> grpstats(thuey,{'person','month'},{'mean','@sum'},'DataVars',{'spending'})
ans =
3×5 table
person month GroupCount mean_spending Fun2_spending
______ ________ __________ _____________ _____________
John_Jan-2020 John Jan-2020 2 25 50
Kate_Jan-2020 Kate Jan-2020 1 30 30
Mike_Jan-2020 Mike Jan-2020 1 15 15
>>
  6 Comments
dpb
dpb on 4 Jul 2020
Edited: dpb on 5 Jul 2020
>> tapled=table(tgrps.person,tgrps.month,tgrps.GroupCount, ...
tgrps.mean_spending,tgrps.Fun2_spending, ...
'VariableNames',{'Individual','Time Period','Number Outlays', ...
'Total Outlay','Average Outlay'})
tapled =
3×5 table
Individual Time Period Number Outlays Total Outlay Average Outlay
__________ ___________ ______________ ____________ ______________
John Jan-2020 2 25 50
Kate Jan-2020 1 30 30
Mike Jan-2020 1 15 15
>>
To write variable text would require building those strings via sprintf or compose or the like...it's doable but probably not a fun thing to code.
NB: To organize by individual instead of month you'd want to reorder the grouping variables or else will have to use varfun or the like to regroup in that order instead.
NB2: Wasn't able to find a way to turn off the 'RowNames' property of the original table once they were written by grpstats; hence the table construction above. Hadn't ever noticed that "feature" before...
dpb
dpb on 5 Jul 2020
Edited: dpb on 5 Jul 2020
>> fmt="Number %s's outlays, total of %s's outlays, average of %s's outlays";
>> compose(fmt,repmat(tapled.Individual,1,3))
ans =
3×1 string array
"Number John's outlays, total of John's outlays, average of John's outlays"
"Number Kate's outlays, total of Kate's outlays, average of Kate's outlays"
"Number Mike's outlays, total of Mike's outlays, average of Mike's outlays"
>>
let's you build the sample string; I don't know just what you really envision as the output you're looking for. The table above seems pretty good to me; I don't see the purpose in the repetition.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!