# Calculate proportions of all returns, returns between a height interval

I want to calculate, for each pixel, the proportions of returns between 0.5 and 5 m height above ground, compared to the total return count

I have figured out how to do it for the entire las file, but how can I for each 10*10 m pixel?

``````library(lidR)
las = readLAS("C:/folder/lasfile.laz")

#Normalize las
dtm <- rasterize_terrain(las, 1, knnidw(k = 6L, p = 2))
nlas <- normalize_height(las, dtm)

#This is working for the entire las file
z <- nlas\$Z
prop.table(table(0.5<=z & z<=5))
#FALSE       TRUE
#0.90185932 0.09814068 #So 10 % of the returns are in the 0.5-5 m height interval

fun1 <- ~list(maxz = max(Z)) #I need to create my own function, I cant get it to work.
m <- pixel_metrics(nlas, fun1, 10)
``````

## 1 Answer

You can use the following function

``````f = function(z) { prop.table(table(0.5<=z & z<=5))[2] }
``````

Then apply it either per point cloud or per pixel

``````cloud_metrics(nlas, ~f(Z))
pixel_metrics(nlas, ~f(Z), 10)
``````

By the way you can also write

``````f = function(z) { sum(0.5<=z & z<=5) / length(z) }
``````