0

I am wondering if there is a way to perform integral on arrays in R. I have arrays S and R. I want to integrate them over pressure level (P) from 1000 to 850. How can I do this?

enter image description here.

The S, R and P data are

S<-structure(c(0.0011979939772106, 0.0011979939772106, 0.0011979939772106, 
0.00122851820731487, 0.00122654890214685, 0.00122457959697883, 
0.00124164690843498, 0.00123705186304294, 0.0012324568176509, 
0.00133617355649982, 0.00133617355649982, 0.00133617355649982, 
0.00138048292278021, 0.00137752896502818, 0.00137457500727616, 
0.00140575567243643, 0.00139951953940438, 0.00139328340637232, 
0.00139820666929237, 0.00139820666929237, 0.00139820666929237, 
0.00151308280409338, 0.00150192340814128, 0.00149076401218919, 
0.00155575108273376, 0.00154426346925366, 0.00153277585577356
), .Dim = c(3L, 3L, 3L))
R<-structure(c(-15.1752538162522, -15.1929331135921, -15.2092524649828, 
-16.2142525214608, -16.2400914944961, -16.2604906837345, -17.2355719293295, 
-17.2641307942633, -17.2858899294509, -13.3842050011216, -13.4059641363092, 
-13.4250033795984, -14.3266475439352, -14.3361671655798, -14.3402470034274, 
-15.3466070058547, -15.3398072761085, -15.3262078166163, -10.7132711568418, 
-10.7350302920294, -10.7554294812678, -11.8379464568517, -11.8066677000195, 
-11.7726690512888, -13.8003484615847, -13.7187517046312, -13.6317151638807
), .Dim = c(3L, 3L, 3L))
P<-c(1000,950,900,850)

I tried the following and could not figure out how I can perform integral for an array.

f <- function(x) {x}
inr <- integrate(f,1000,850) #where f would be a function.
Cirrus
  • 638
  • 3
  • 13
  • 26

1 Answers1

0

I am not entirely sure what you are asking, but there are two common answers when dealing with arrays and integration. The first one is a vectorization issue which can be dealt with by doing the following:

IntFunc <- function(x,y) {
  sum(x-y)
}
IntFunc(1:5,c(0,0))
Warning message:
  In x - y : 
  longer object length is not a multiple of shorter object length
integrate(Vectorize(IntFunc,vectorize.args = 'x'), upper = 1000, lower = 850, y = R)
3803862 with absolute error < 4.2e-08

This question has been answered elsewhere on StackOverflow:

How to pass vector to integrate function

R, Integrate at each point of array

Community
  • 1
  • 1
josh453
  • 308
  • 2
  • 12