1

I have a dataset with +1000 rows and I would compute the area under the line of each row. For example, suppose I have a dataset like this:

df<-rbind(c(0, 2, 0, 3, 0),
          c(2, 2, 0, 1, 0))

For the first row, I would compute the area under the line in this figure: enter image description here

which would be

> 2*2/2+2*3/2
[1] 5

And for the second row, it would be

> 1*2+1*2/2+2*1/2
[1] 4

enter image description here

I'm wondering whether there is an easy way to realize it in R?

David Z
  • 5,931
  • 8
  • 44
  • 87

1 Answers1

2

Try pracma package

library(pracma)
trapz(df[1,])
Fedorenko Kristina
  • 2,257
  • 2
  • 18
  • 17