I want to write recursive function for calculating the arithmetic mean of a vector.
For $X(n)=\{1,2,3,\ldots, n\}$, the formula is ${\rm Mean}(X(n))=((n-1)\, {\rm Mean}((X(n-1))+X_n)/n$.
My program works in my R-language version, but here in Mathematica it takes in account only the last two number. How can I fix my code?
Meen[x_] := (
If[Length[x] == 1,
Return[x],
n = Length[x] - 1;
y = x[[1 ;; n]];
result = 1/(n + 1)*(n*Meen[x[[1 ;; n]]] + x[[n + 1]])]
)
X = {1, 2, 3, 4, 5};
Meen[X]
{9/5}