I'm trying to create a function that will calculate an Ht-Index for Quantifying the Fractal or Scaling Structure of Geographic Features (but am new to R)
which basically just finds nested means of continuous data (raster in this case) and separates them into 2 groups: heads and tails (values above and below the means). The H-Index is the number of times (-1) the 'heads' group can be subdivided without being greater than some threshold (e.g., 45%). I'm not sure how to efficiently do this in R but here is my failed attempt.
require(raster)
foo <- raster('/foobar.img')
hindex <- function(x){
repeat{
head <- x[x>mean(x)]
tail <- x[x<mean(x)]
headpct <- length(head)/sum(length(head),length(tail))
if (headpct >.45) break # If head is > 45% of values stop
else
if (length(head)== 1) break # if head is only one value stop
}
return() # I NEED THIS TO RETURN NUMBER OF REPEATS - 1
}
Pretty straightforward problem. Any help would be greatly appreciated, especially with the number of iterations to return and syntax. Not sure if question would be better migrated to GIS