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

 Respuesta aceptada

Peter Perkins
Peter Perkins el 16 de Nov. de 2017

0 votos

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 comentarios

UQFG
UQFG el 16 de Nov. de 2017
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);
Peter Perkins
Peter Perkins el 16 de Nov. de 2017
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 el 16 de Nov. de 2017
Editada: UQFG el 16 de Nov. de 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')

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 13 de Nov. de 2017

Editada:

el 16 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by