The function I defined returns a different result than the R function.
n <- 100
set.seed(42) ## for sake of reproducibility
x <- rnorm(n)
mean(x > 0)
# [1] 0.54
aver <- function(x, n) {
h <- 0
j <- 0
for (i in x) {
if (i > 0) {
h <- h + i
j <- j + 1
}
}
return(h/j)
}
aver(x, 0)
# [1] 0.7949533
What is the problem?