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개)

카테고리

도움말 센터File Exchange에서 Repeated Measures and MANOVA에 대해 자세히 알아보기

태그

질문:

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