Given the following dataframe:
mydf <- data.frame(x=c(1:10,10:1),y=c(10:1,1:10))
How is it possible to
split it such that each sub-dataframe will have consecutive values of one column which are greater than the other column?
For example in
mydf, the outcome that I am hoping for is
spliting it into three dataframes:
- (y > x; should contain the first 5 rows of
- (x > y; should contain rows 6 to 15 of
- (y > x again; should contain the last 5 rows of
I tried using the following code but it produced bad results where each y > x would be split individually; moreover, dataframes where x > y would contain a y > x in the first row:
split(mydf, cumsum(mydf$x > mydf$y))
Another less elegant approach I tried to do is
sapply with individual
ifs inside the
split function, but I don't want to go this path because of performance issues.