```
# NOT RUN {
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
las = readLAS(LASfile)
col = height.colors(50)
# === Using all points ===
# Mean height with 400 m^2 cells
metrics = grid_metrics(las, ~mean(Z), 20)
plot(metrics, col = col)
# Define your own new metrics
myMetrics = function(z, i) {
metrics = list(
zwimean = sum(z*i)/sum(i), # Mean elevation weighted by intensities
zimean = mean(z*i), # Mean products of z by intensity
zsqmean = sqrt(mean(z^2))) # Quadratic mean
return(metrics)
}
metrics = grid_metrics(las, ~myMetrics(Z, Intensity))
plot(metrics, col = col)
#plot(metrics, "zwimean", col = col)
#plot(metrics, "zimean", col = col)
# === With point filters ===
# Compute using only some points: basic
first = filter_poi(las, ReturnNumber == 1)
metrics = grid_metrics(first, ~mean(Z), 20)
# Compute using only some points: optimized
# faster and uses less memory. No intermediate object
metrics = grid_metrics(las, ~mean(Z), 20, filter = ~ReturnNumber == 1)
# Compute using only some points: best
# ~50% faster and uses ~10x less memory
las = readLAS(LASfile, filter = "-keep_first")
metrics = grid_metrics(las, ~mean(Z), 20)
# }
```

Run the code above in your browser using DataLab