Calculating interval of grouped data

Viewed 48

I am looking for the interval between two peaks, per group in my data. To me it sounds like a very easy thing to do, but I can't get it to work. Since I am quite new to coding, it can be that I just don't know the right terms to find the answer.

My data looks like this, where I want to group the data by Testnr and index is the time at which the peak occurs.

   Sensor0 Treatment Testnr RPM index
1 908.9414      T1_S  T1_1S 780   332
2 946.9633      T1_S  T1_1S 780   670
3 933.7463      T1_S  T1_1S 780  1008
4 928.3887      T1_S  T1_4S 780  1347
5 900.3490      T3_S  T3_3S 780   685
6 922.8129      T4_S  T4_2S 780    23

I have tried

> pilott0%>%
   group_by(Testnr)%>%
   for (i in nrow(Pilott0)) {
     pilott0$waveperiod=pilott0$index[i]-pilott0$index[i-1]  }

Error in for (. in i) nrow(Pilott0):4 arguments passed to 'for' which requires 3

And when I tried:

> for (i in 2:nrow(pilott0)) {
    pilott0$waveperiod=pilott0$index[i]-pilott0$index[i-1] }

   Sensor0 Treatment Testnr RPM index waveperiod
1 890.2380      T1_S  T1_1S 780   298        398
2 928.1808      T1_S  T1_1S 780   636        398
3 923.4647      T1_S  T1_1S 780   975        398
4 929.1885      T1_S  T1_1S 780  1313        398
5 885.0237      T1_S  T1_1S 780  1652        398
6 918.6435      T1_S  T1_1S 780  1988        398

Which is strange since 636-298=338 and not 398.

0 Answers
Related