I have a dataset looking like this:
Var1 <- c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
Var2 <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4)
value <- c(0.000, 7906.818, 27243.707, 26824.343, 7906.818, 0.000, 34109.213, 34162.103, 27243.707, 34109.213, 0.000, 4172.058, 26824.343, 34162.103, 4172.058, 0.000)
d <- as.data.frame(cbind(Var1, Var2, value))
> d
Var1 Var2 value
1 1 1 0.000
2 2 1 7906.818
3 3 1 27243.707
4 4 1 26824.343
5 1 2 7906.818
6 2 2 0.000
7 3 2 34109.213
8 4 2 34162.103
9 1 3 27243.707
10 2 3 34109.213
11 3 3 0.000
12 4 3 4172.058
13 1 4 26824.343
14 2 4 34162.103
15 3 4 4172.058
16 4 4 0.000
And what I’m looking for is a way to remove consecutive values in the two columns, that is, e.g. if Var1=1
and Var2=2
, or Var1=2
and Var2=3
, those rows should be removed. This would be included in a loop, as there are multiple sequences like this one.
The output dataframe would be like this:
Var1 Var2 value
1 1 1 0.000
2 2 1 7906.818
3 3 1 27243.707
4 4 1 26824.343
6 2 2 0.000
7 3 2 34109.213
8 4 2 34162.103
9 1 3 27243.707
11 3 3 0.000
12 4 3 4172.058
13 1 4 26824.343
14 2 4 34162.103
16 4 4 0.000
Any thoughts?
Thanks in advance!
>Solution :
Try:
d[ d$Var2 - d$Var1 != 1, ]