Removing rows in dataframe based on values ​​in several previous rows / columns

I have the following framework:

   x  y  z
1  a  c  0
2  a  c  0
3  a  c  1
4  a  c  0
5  a  c  0
6  b  c  0
7  b  c  0
8  b  c  0
9  b  c  1
10 b  c  0
11 b  c  0
12 b  c  0
13 a  d  0
14 a  d  0
15 a  d  0

I would like to delete rows for which there is 1 in the previous row of column z with the same values ​​in columns x and y. For example, for Row 10, I want to look for lines 1: 9 for a line in which x = "b", y = "c", and z is 1. If such a line exists in lines 1: 9, I want to delete line 10 .

Therefore, the resulting frame will delete lines 4, 5, 10, 11, and 12:

   x  y  z
1  a  c  0
2  a  c  0
3  a  c  1
4  b  c  0
5  b  c  0
6  b  c  0
7  b  c  1
8  a  d  0
9  a  d  0
10 a  d  0
+4
source share
3 answers

We can do it with data.table

library(data.table)
setDT(df1)[-df1[, .I[cummin(c(0, diff(z==1)))<0], .(x, y)]$V1]
#    x y z
# 1: a c 0
# 2: a c 0
# 3: a c 1
# 4: b c 0
# 5: b c 0
# 6: b c 0
# 7: b c 1
# 8: a d 0
# 9: a d 0
#10: a d 0
+3
source

R ave , interaction . as.logical ave, 1s 0s , .

c(1,head(cummin(i != 1), -1)) 1 , . , 1 , 1 0 . head , .

df[as.logical(ave(df$z, interaction(df$x, df$y),
                  FUN=function(i) c(1,head(cummin(i != 1), -1)))), ]
   x y z
1  a c 0
2  a c 0
3  a c 1
6  b c 0
7  b c 0
8  b c 0
9  b c 1
13 a d 0
14 a d 0
15 a d 0
+2

, , , z = 1,

which(nameofdataframe$z != 1)

, , :

which(nameofdataframe$z != 1 & nameofdataframe$x == "b")

, !

0

Source: https://habr.com/ru/post/1675929/


All Articles