0

I have a data base with 121 rows and like 10 columns. One of these columns corresponds to Station, another to depth and the rest to chemical variables (temperature, salinity, etc.). I want to calculate the integrated value of these chemical properties by station, using the function oce::integrateTrapezoid. It's my first time doing a loop, so i dont know how. Could you help me?

dA<-matrix(data=NA, nrow=121, ncol=3)
for (Station in unique(datos$Station))
{dA[Station, cd] <- integrateTrapezoid(cd, Profundidad..m., "cA")
  }
Station Depth temp
1 10 28
1 50 25
1 100 15
1 150 10
2 9 27
2 45 24
2 98 14
2 152 11
3 11 28.7
3 48 23
3 102 14
3 148 9
mck
  • 40,932
  • 13
  • 35
  • 50
Alejandra
  • 1
  • 1
  • You probably don't need a loop but `by` (from `base`) or `group_by` (from `dplyr`). I am not sure about your code, where does that `cd` comes from? What result/error do you get? – Claudio May 31 '21 at 09:21
  • Cd is a another variable, like temperature or salinity... I don't get any error.... For example, the integration that a I want, it should be done considering the temperature values ​​at 10 and 50 m, and then at 50 to 100 m, and then from 100 to 150m, for the station 1. But what I get from the code is, after doing the station 1, the next integrated temperature value is made with the value at​​ 150m from the first station and the value at 9 m from the second station.... that's why a i want to separete by group of stations. – Alejandra May 31 '21 at 23:02
  • So you want to apply a function *incrementally* over the rows, by group? So for example if the function was just a sum, for `temp` you would expect 28, 53, 68, 78 for Station 1 and 27, 51, 65, 76 for station 2, and so on? – Claudio Jun 01 '21 at 15:54

0 Answers0