Computing volumes

I'm trying to calculate some volumes using R. Imagine I have the following data:

h=seq(from=663,by=0.1,length.out=11); #Vector of reference heights
Ap=0.25; #Discretization area
z=c(NA,NA,NA,663.05,663.13,663.25,663.33,663.45,663.67,663.84,663.9,664.39,NA,NA,NA); #Elevation of every element in the surface

What I want is to generate a vector returning a Riemann sum, computing the product Ap*(h-z) for every element in z<h.

Does anyone know how this can be achieved using R?

I have tried using sapply(), but I haven't been successful so far.

Thank you in advance for any help!

I am not at all sure I understand what you want to do. Is it to calculate for every element of h the sum of Ap* (h[i] -z) but using only cases where h[i] > z?

h=seq(from=663,by=0.1,length.out=11); #Vector of reference heights
Ap=0.25; #Discretization area
z=c(NA,NA,NA,663.05,663.13,663.25,663.33,663.45,663.67,663.84,663.9,664.39,NA,NA,NA)
#z <- z[!is.na(z)]
sapply(h, function(x) sum(ifelse(x > z, Ap * (x - z), 0), na.rm = TRUE))
#>  [1] 0.0000 0.0125 0.0550 0.1175 0.2100 0.3225 0.4475 0.5800 0.7300 0.8950
#> [11] 1.0950

Created on 2019-09-23 by the reprex package (v0.2.1)

Just to give another solution that I have found:

Ap*sapply(h,function(x) sum((z<x)*(x-z),na.rm=TRUE))

Returning the same result as in the previous answer.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.