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.

 Respuesta aceptada

dpb
dpb el 3 de Jul. de 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 comentarios

alpedhuez
alpedhuez el 3 de Jul. de 2020
Editada: alpedhuez el 4 de Jul. de 2020
Not exactly asked in the question.
dpb
dpb el 4 de Jul. de 2020
Well, you'll have to do some work yourself, then.
grpstats does what grpstats does...
alpedhuez
alpedhuez el 4 de Jul. de 2020
create a table for John. rename the column name. do it also for Kate and Mike. Then merge all three tables?
dpb
dpb el 4 de Jul. de 2020
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 el 4 de Jul. de 2020
Editada: dpb el 5 de Jul. de 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 el 5 de Jul. de 2020
Editada: dpb el 5 de Jul. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 3 de Jul. de 2020

Editada:

dpb
el 5 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by