This question already has an answer here:

I have am trying to obtain the average of the distance from the mean radius for each shot in a group of shots at a target. I am having problems with the very last line. The output of the last line appears that the Apply statement is being applied to the correct level but it is not resolving.


ShotsPerGroup = 3  
NumberGroups = 2  
(* Define the group of shots as a random variable with a normal distribution *)  
RandomSeed[66936, Method -> All]  
R := RandomVariate[NormalDistribution[]];  
a = Flatten[
   Table[List[i, R], {i, 1, NumberGroups}, {j, 1, ShotsPerGroup}], 
Print["a = ", TableForm[a]]  
(* Group the shots by group *)  
byGroup = GatherBy[a, First][[All, All, 2]];  
Print["byGroup =", byGroup]  
(* Calculate the distance from the mean radius of each shots by group *)  
mr = Sqrt[(#^2 + Mean[#]^2)] & /@ byGroup  
Print["mr = ", TableForm[mr]]  
(* obtain the mean of the group mean radius *)  
meanMR = 
 Apply[Mean, Sqrt[(#^2 + Mean[#]^2)] & /@ byGroup, 1]  

output from last line:

{Mean[1.1711, 0.717469, 0.980774], Mean[0.631837, 1.55101, 0.573568]} 

marked as duplicate by LLlAMnYP, rcollyer, C. E., m_goldberg, dr.blochwave Sep 22 '15 at 11:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ The second argument of Apply matches mr. Why not just stick that in? You are also confusing the use cases of Map (short form - /@) and Apply. meanMR = Mean /@ mr returns {1.64748, 1.13594}. $\endgroup$ – LLlAMnYP Sep 22 '15 at 10:26
  • $\begingroup$ i swear i tried that! Thank you. $\endgroup$ – Steven Anderson Sep 22 '15 at 10:30

Somewhat more detailed answer, though this question "arises due to a simple mistake..."

Mean takes a list as its argument. What you had was {{a, b, c}, {d, e, f}}. The FullForm of this is:

List[List[a, b, c], List[d, e, f]]

Apply at level 1 replaces the heads of list at level 1 like so:

List[Mean[a, b, c], Mean[d, e, f]]

while what you needed, was:

List[Mean[List[a, b, c]], Mean[List[d, e, f]]]

which is exactly the result of Mapping.

  • $\begingroup$ Thank you for taking the time to answer in detail. That was very helpful! $\endgroup$ – Steven Anderson Sep 22 '15 at 16:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.