This post is motivating and based on the original post which involved defining control limits. In the following I have not merely used the sample mean by day as the replacement mean (for convenience,this is not what was posted but could be adapted ). In the small samples (n=1), StandardDeviation
will not work, hence the first line. I agree with comments re: DayName
.
EDIT
Revision with only one pass of DayName
on data:
rout[d_, l_, u_] := Module[{dn, lmt, h, f, g, result},
dn = {DayName[#1], #1, ##2} & @@@ d;
lmt = {First@#1, Mean@#3 - l sd[#3], Mean@#3 + u sd[#3],
Mean[#3]} & @@@ (Transpose /@ GatherBy[dn, #[[1]] &]);
(h[#1] := #4) & @@@ lmt;
(f[#1] = Function[x, #2 < x < #3]) & @@@ lmt;
g = MapThread[#1@#2 &, {(f[DayName[#]] & /@ d[[All, 1]]),
d[[All, 2]]}];
result =
Thread[{dn, g}] /. {{x_, y_, z_}, False} :> {{x, y, h@x, z},
"Edited"}
]