# Mymetrics LidR canopy cover

I am trying to compute my own metric using LidR. I would like to compute the canopy cover metric as `(# of first returns > 2m) / (total number of first returns)`

The package suggested this function from the Examples to compute this metric

``````myMetrics = function(z,rn){
first  = rn == 1L
zfirst = z[first]
nfirst = length(zfirst)
above2 = sum(z> 2)
above2
x =(above2/nfirst)*100
x
# User's metrics
metrics = list(
above2aboven1st = x, # Num of returns above 2 divided by num of 1st returns
zsqmean = sqrt(mean(z^2))  # Quadratic mean of z
)
metrics
# Combined with standard metrics
return( c(stdmetrics_z(z),metrics))
}

metrics = grid_metrics(las, ~myMetrics(Z, rn=ReturnNumber))
``````

However, the results of the metric using this function does not work correctly, since my values range between 0 and 300

it would be possible to calculate a density metric in height intervals as in FUSION or Lastools?

e.g number of all returns between 0.2 and 3 meters/number of total returns in 20 meters cell

You are not computing `(# of first returns > 2m) / (total number of first returns)` but instead `(# of returns > 2m) / (total number of first returns)`. Did you get this for the documentation. If yes it is a mistake in the doc.

``````myMetrics = function(z,rn){
first  = rn == 1L
zfirst = z[first]
nfirst = length(zfirst)
firstabove2 = sum(zfirst > 2)
x = (firstabove2/nfirst)*100
metrics = list(
above2aboven1st = x, # Num of returns above 2 divided by num of 1st returns
zsqmean = sqrt(mean(z^2))  # Quadratic mean of z
)
metrics
# Combined with standard metrics
return( c(stdmetrics_z(z),metrics))
}
``````
• it would be possible to calculate a density metric in height intervals as in FUSION or Lastools? e.g number of all returns between 0.2 and 3 meters/number of total returns in 20 meters cell Feb 6, 2020 at 11:01
• Everything is possible actually. You just have to write the good function.
– JRR
Feb 6, 2020 at 12:31