Q:

Calculation of density metric of 0.75 height quantile based on Airborne LiDAR Scanning point cloud data by lidR package

This is a piece of code that I wrote: 
metrics<-function(z){Q75=quantile(z,probs=c(0.75))         

aboveq75=sum(z>Q75)

dh75=aboveq75/length(z)

return(dh75)}

DH75<-grid_metrics(lid,~metrics(Z),res = 30)

I want to calculate the proportion of point cloud quantity above 0.75 height quantile in the total point cloud quantity based on 30-meter grid scale. This metrics can be used to participate in the construction of a forest inventory attributes prediction model using an area-based approach.Because it reflects the distribution of branches and leaves in the upper and middle canopy.But the number of points above the 0.75 quantile is by definition always going be approximately 0.25.Is my definition wrong? or you have a better idea about the Calculation of density metric of 0.75 height quantile.
The result of my definition:

The corresponding value of the grid:

A:

The number of points above the 0.75 quantile is by definition always going be approximately 0.25. The variation you are getting is because if there's only a small number of points then the quantile computation is approximate or there's tied values. Compare:
> metrics(runif(1000))
[1] 0.25
> metrics(runif(12))
[1] 0.25
> metrics(runif(13))
[1] 0.2307692

The value for metrics(12) is exactly 0.25 because 3 are greater than it and 9 are less than or equal to it, and 3/12 is 0.25 - but with 13 values you've got 3 greater than it and 10 less then or equal, producing the value 3/13 = 0.2307692 as seen.
If instead you want the number of points in each grid square with Z values greater than the 0.75 quantile over all the points in the cloud, compute your threshold outside the function from all the Z values. Here's a reproducible example using sample data from the lidR package:
LASfile <- system.file("extdata", "Megaplot.laz", package="lidR")
las = readLAS(LASfile)
Q75=quantile(las$Z,0.75)
Q75
##   75% 
## 19.32 

Then use your metrics function minus that one line:
metrics <- 
  function(z){
   aboveq75=sum(z>Q75)
   dh75=aboveq75/length(z)
   return(dh75)
  }

Then run as before - I'm using a 3m grid here because the data is different:
> DH75<-grid_metrics(las,~metrics(Z),res = 3)
> plot(DH75)

