Home > Software design >  Sequential & conditional row deletion R
Sequential & conditional row deletion R

Time:12-08

I need to remove rows in this dataset until there is no TRUE within FALSE or vice-versa or in order words the all subsequent rows are the same (yes, except the boundary). The number of row deletions should be the lowest possible.

For example, by deleting rows which counter == 9 in the dataset below, more consecutive rows with TRUE value will be formed. I want the loop to identify the lowest frequency value in counter and remove these rows progressively, until there is no FALSE within TRUE and vice-versa. We also could remove all rows which counter == 4, 5, and 6 because they also occur only twice. Note that after these rows are removed, larger "blocks" or "chunks" of TRUEs and FALSEs are formed, which should be accounted for future deletions.

Here is the dataset:

    inter counter
189 FALSE       1
192 FALSE       1
233 FALSE       1
235 FALSE       1
237 FALSE       1
238 FALSE       1
249 FALSE       1
256 FALSE       1
258 FALSE       1
259 FALSE       1
14  FALSE       1
17  FALSE       1
36  FALSE       1
39  FALSE       1
82  FALSE       1
114 FALSE       1
117 FALSE       1
136 FALSE       1
152 FALSE       1
194 FALSE       1
212 FALSE       1
215 FALSE       1
251 FALSE       1
262 FALSE       1
267 FALSE       1
268 FALSE       1
57   TRUE       2
60   TRUE       2
96   TRUE       2
99   TRUE       2
232  TRUE       2
239  TRUE       2
242  TRUE       2
260  TRUE       2
19  FALSE       3
41  FALSE       3
119 FALSE       3
217 FALSE       3
62   TRUE       4
101  TRUE       4
181 FALSE       5
206 FALSE       5
244  TRUE       6
269  TRUE       6
176 FALSE       7
179 FALSE       7
201 FALSE       7
204 FALSE       7
157 FALSE       7
159 FALSE       7
72   TRUE       8
74   TRUE       8
126  TRUE       8
128  TRUE       8
142  TRUE       8
144  TRUE       8
172  TRUE       8
178  TRUE       8
180  TRUE       8
182  TRUE       8
183  TRUE       8
186  TRUE       8
203  TRUE       8
205  TRUE       8
207  TRUE       8
208  TRUE       8
175 FALSE       9
200 FALSE       9
9    TRUE      10
11   TRUE      10
31   TRUE      10
33   TRUE      10
109  TRUE      10
111  TRUE      10
174  TRUE      10
199  TRUE      10
52   TRUE      10
54   TRUE      10
91   TRUE      10
93   TRUE      10
177  TRUE      10
184  TRUE      10
202  TRUE      10
209  TRUE      10
227  TRUE      10
222  TRUE      10
225  TRUE      10
161  TRUE      10
76   TRUE      10
130  TRUE      10
146  TRUE      10
188  TRUE      10
224  TRUE      10
226  TRUE      10
228  TRUE      10
229  TRUE      10
13   TRUE      10
35   TRUE      10
113  TRUE      10
211  TRUE      10
56   TRUE      10

Here is the code.

structure(list(inter = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE), counter = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L)), class = "data.frame", row.names = c(189L, 
192L, 233L, 235L, 237L, 238L, 249L, 256L, 258L, 259L, 14L, 17L, 
36L, 39L, 82L, 114L, 117L, 136L, 152L, 194L, 212L, 215L, 251L, 
262L, 267L, 268L, 57L, 60L, 96L, 99L, 232L, 239L, 242L, 260L, 
19L, 41L, 119L, 217L, 62L, 101L, 181L, 206L, 244L, 269L, 176L, 
179L, 201L, 204L, 157L, 159L, 72L, 74L, 126L, 128L, 142L, 144L, 
172L, 178L, 180L, 182L, 183L, 186L, 203L, 205L, 207L, 208L, 175L, 
200L, 9L, 11L, 31L, 33L, 109L, 111L, 174L, 199L, 52L, 54L, 91L, 
93L, 177L, 184L, 202L, 209L, 227L, 222L, 225L, 161L, 76L, 130L, 
146L, 188L, 224L, 226L, 228L, 229L, 13L, 35L, 113L, 211L, 56L
))

CodePudding user response:

Here is one possible answer:

library(data.table)

for(i in 1:nrow(dt)) {           
if(length(rle(dt$inter)$lengths)>2)
dt$counter<-rleid(dt$inter)
dt<-dt[!dt$counter %in% names(which(table(dt$counter) == min(table(dt$counter)))), ]
dt$counter<-rleid(dt$inter)
if(length(rle(dt$inter)$lengths)==2) 
break}
  • Related