splitapply table for zscore

I have a table in the format:
Group Score
A 3
B 7
C 18
A 10
How do I combine the splitapply function for table variables with zscore , such that it calculates zscore(Score) according to Group rather than the whole table?
I have tried:
[G,results] = findgroups(Table.Group);
ZS = splitapply(@zscore,Table.Score,G);
results.ZS = ZS

 Accepted Answer

splitapply is more for computing scalar summaries. Try using varfun, with a grouping variable, returning a table. Something like
t2 = varfun(@zscore,t,'GroupingVariable','Group')

3 Comments

Thanks, that does seem to be appropriate... however, when I try to apply it, I get the following errors:
Error using getGroupingVarOrTime (line 13) Unrecognized variable name 'Table.Group'.
Error in tabular/varfun (line 130) groupVars = getGroupingVarOrTime(groupVars, a);
I'm guessing you have a table named 'Table' (which is probably not a great idea), and a variable in it named 'Group'. The error is telling you that you do not have a variable named 'Table.Group'.
UQFG
UQFG on 16 Nov 2017
Edited: UQFG on 16 Nov 2017
I actually don't... I just used those names as generic examples!
I just realised that you don't need the "Table." in front of the Group name... so the code that works is:
ZS=varfun(@zscore,Table,'GroupingVariables','Group')
Whereas what I had before was:
ZS=varfun(@zscore,Table,'GroupingVariables','Table.Group')

Sign in to comment.

More Answers (0)

Categories

Asked:

on 13 Nov 2017

Edited:

on 16 Nov 2017

Community Treasure Hunt

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

Start Hunting!