I have a dataframe as the following :
COL_1 COL_2 COL_3 COL_4 COL_5 COL_6
<int> <int> <int> <int> <int> <int>
1 1 1 1 1 1 1
2 1 1 1 1 1 2
3 1 1 1 1 1 3
4 1 1 1 1 1 4
5 1 2 1 1 1 5
6 1 1 1 1 1 6
7 1 3 4 5 6 7
8 1 1 1 1 1 8
9 1 1 9 1 1 9
10 1 3 5 7 9 10
I'd like to filter this dataset to keep only values from COL_1
to COL_6
strictly increasing, so it would be as the following:
COL_1 COL_2 COL_3 COL_4 COL_5 COL_6
<int> <int> <int> <int> <int> <int>
7 1 3 4 5 6 7
10 1 3 5 7 9 10
EDIT : The code should be used in a function with a dynamic number of columns (which will be named from COL_1
to COL_N
). A "basic" code such as
df %>% filter(COL_6 > COL_5 & ... & COL_2 > COL_1)
will not work in my situation. Thank you very much
With a bit of work, you could use Reduce for this. For example
keep <- Reduce(function(x, y) {
list(y, x[[2]] & (x[[1]] < y))
}, dd, init=list(dd[[1]]-1, TRUE))[[2]]
which(keep)
# [1] 7 10
dd[keep, ]
# COL_1 COL_2 COL_3 COL_4 COL_5 COL_6
# 7 1 3 4 5 6 7
# 10 1 3 5 7 9 10
Tested with
dd <- read.table(text="
COL_1 COL_2 COL_3 COL_4 COL_5 COL_6
1 1 1 1 1 1 1
2 1 1 1 1 1 2
3 1 1 1 1 1 3
4 1 1 1 1 1 4
5 1 2 1 1 1 5
6 1 1 1 1 1 6
7 1 3 4 5 6 7
8 1 1 1 1 1 8
9 1 1 9 1 1 9
10 1 3 5 7 9 10", header=TRUE)