how to user grpstats function in this case

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.

 採用された回答

dpb
dpb 2020 年 7 月 3 日

0 投票

>> 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 件のコメント

alpedhuez
alpedhuez 2020 年 7 月 3 日
編集済み: alpedhuez 2020 年 7 月 4 日
Not exactly asked in the question.
dpb
dpb 2020 年 7 月 4 日
Well, you'll have to do some work yourself, then.
grpstats does what grpstats does...
alpedhuez
alpedhuez 2020 年 7 月 4 日
create a table for John. rename the column name. do it also for Kate and Mike. Then merge all three tables?
dpb
dpb 2020 年 7 月 4 日
You've got all the results in the above table; just rearrange the pieces and output in the format wanted.
It's essentially your desired table with the added group label column of both person and month.
dpb
dpb 2020 年 7 月 4 日
編集済み: dpb 2020 年 7 月 5 日
>> 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 2020 年 7 月 5 日
編集済み: dpb 2020 年 7 月 5 日
>> 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.

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

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2020 年 7 月 3 日

編集済み:

dpb
2020 年 7 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by