Split a data frame based on difference between values in different rows
r, split
Solution
You could use `diff` to find the differences between consecutive values and `split` to split the data frame. Assuming your data frame is called `dat`:
# create an index for differences > 2
idx <- c(0, cumsum(abs(diff(dat$MIN)) > 2))
# split the data frame
split(dat, idx)
The result (a list of 4 data frames):
$`0`
MIN SEC PT CO2R CO2D PAR
1 58 10 5 375.7 -11.6 1002
2 58 20 5 375.4 -11.6 1001
3 58 33 5 375.2 -11.6 1001
4 58 43 5 375.2 -11.5 1000
5 58 54 5 375.3 -11.8 1000
$`1`
MIN SEC PT CO2R CO2D PAR
6 2 0 5 375.5 -6.3 1001
7 2 8 5 375.3 -6.0 1000
8 2 21 5 375.2 -6.1 997
9 2 37 5 375.3 -6.2 993
10 2 51 5 375.4 -6.2 1003
$`2`
MIN SEC PT CO2R CO2D PAR
11 5 20 5 376.3 -7.6 1000
12 5 35 5 376.1 -7.3 1000
13 5 52 5 375.9 -7.3 1000
14 6 5 5 376.0 -7.8 1000
15 6 23 5 376.1 -8.0 1002
$`3`
MIN SEC PT CO2R CO2D PAR
16 10 2 5 376.3 -3.3 1003
17 10 14 5 376.3 -3.1 1003
18 10 27 5 376.5 -3.4 1003
19 10 41 5 376.7 -3.7 1006
20 10 55 5 376.8 -3.9 997
Problem
I am not sure I am approaching this in the correct way, but what I am trying to do is split up a data frame into groups based on the difference between values. For example, using the data below I would like to split on the difference between values in the MIN column, so if the difference is >2 then create a split, in the example below I would end up with 4 split sets of data. ``` MIN SEC PT CO2R CO2D PAR 58 10 5 375.7 -11.6 1002 58 20 5 375.4 -11.6 1001 58 33 5 375.2 -11.6 1001 58 43 5 375.2 -11.5 1000 58 54 5 375.3 -11.8 1000 2 0 5 375.5 -6.3 1001 2 8 5 375.3 -6 1000 2 21 5 375.2 -6.1 997 2 37 5 375.3 -6.2 993 2 51 5 375.4 -6.2 1003 5 20 5 376.3 -7.6 1000 5 35 5 376.1 -7.3 1000 5 52 5 375.9 -7.3 1000 6 5 5 376 -7.8 1000 6 23 5 376.1 -8 1002 10 2 5 376.3 -3.3 1003 10 14 5 376.3 -3.1 1003 10 27 5 376.5 -3.4 1003 10 41 5 376.7 -3.7 1006 10 55 5 376.8 -3.9 997 ``` I have used the split function previously when there is unique element to each subset of data, however I have nothing unique in this data set from which to split. Perhaps this function is not what I need? Any hints appreciated! Thanks,