Without looking too deeply into what you are doing, returning the number of iterations is fairly straight forward. I have now made it so that infinite loops don't happen and made the threshold one of the function parameters.
hindex <- function(x, threshold){
iCounter <- 0 # Make counter 0
repeat{
# does one of these have to be an ... equal to eg >= or <=?
head <- x[x>mean(x)]
tail <- x[x<mean(x)]
headpct <- length(head)/sum(length(head),length(tail))
if (headpct >.45 threshold) break # If head is > 45%threshold of values stop
else
if (length(head)== 1) break # if head is only one value stop
x <- head # make x head for nextreset iterationX
iCounter <- iCounter + 1 # Only increment if break doesn't occur
}
return(iCounter)
}