# Problem with Apply [duplicate]

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.

code

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}],
1];
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]}

• 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}. Sep 22, 2015 at 10:26
• i swear i tried that! Thank you. Sep 22, 2015 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.

• Thank you for taking the time to answer in detail. That was very helpful! Sep 22, 2015 at 16:27