I am trying to calculate z-statistic over regular interval of rows.
mean = 77
std = 31
samp.45 = rnorm(45,mean,std)
z.test = function(a, mu, sd){
zeta = (mean(a) - mu) / (sd / sqrt(length(a)))
return(zeta)
}
z.hypothesis = function(a, mu, sd){
z.stat = z.test(a,mu,sd)
if(abs(z.stat)>1.96){
return(1)
}
else{
return(0)
}
}
group = as.numeric(ceiling(1:45/15))
df <- as.data.frame(cbind(samp.45, group))
## Correct this
tapply(df$samp.45, as.factor(df$group), z.hypothesis(df$samp.45,mean,std))
I was planning to use tapply to perform function calculation for each group and return the output. I know that simple functions like mean can be applied directly and give the desired result, but how can I get a similar output for my own function? Any other approach is also welcome.
> tapply(df$samp.45, as.factor(df$group), mean)
1 2 3
78.19556 79.65747 68.91818