In the below data.table, I want to remove all the NA values till the time at least two consecutive numeric values are encountered in column y
. As per this condition, all rows from 1 to 9 should be removed from the table below.
Since I am cleaning a large number of groups, it will be great if the solution considers the performance part.
temp_dt = structure(list(group = c("B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B"), x = c(940, 960,
980, 1000, 1040, 1060, 1080, 1100, 1100, 1120, 1140, 1160, 1180,
1190, 1200, 1200, 1220, 1240, 1260, 1280, 1300, 1300, 1320, 1340,
1360, 1380, 1400, 1400, 1410, 1420), y = c(NA, NA, NA, NA, NA,
NA, NA, 0.525, NA, 2.425, 2.425, NA, NA, NA, 2.425, NA, 2.425,
1.975, 2.45, 2.45, 1.2, NA, NA, 2.275, 2.375, 2.75, 2.675, NA,
2.75, 3.05)), row.names = c(NA, -30L), class = c("data.table",
"data.frame"))
group x y
1: B 940 NA
2: B 960 NA
3: B 980 NA
4: B 1000 NA
5: B 1040 NA
6: B 1060 NA
7: B 1080 NA
8: B 1100 0.525
9: B 1100 NA
10: B 1120 2.425
11: B 1140 2.425
12: B 1160 NA
13: B 1180 NA
14: B 1190 NA
15: B 1200 2.425
16: B 1200 NA
17: B 1220 2.425
18: B 1240 1.975
19: B 1260 2.450
20: B 1280 2.450
21: B 1300 1.200
22: B 1300 NA
23: B 1320 NA
24: B 1340 2.275
25: B 1360 2.375
26: B 1380 2.750
27: B 1400 2.675
28: B 1400 NA
29: B 1410 2.750
30: B 1420 3.050
group x y
I think the solution starts with making group of NA values but don't know how to proceed further -
temp_dt[, na_grp := rleid(is.na(y)), by = group]
Following code partially solves my problem, the only part not solved is of two consecutive values -
temp_dt[!is.na(y) & !na_grp %in% c(1L)]
CodePudding user response:
You could use shift
to test two consecutive values, and cumsum
to hold the selection once initial trigger condition is met:
temp_dt[,.SD[cumsum(!is.na(y)&!is.na(shift(y,-1)))>0],by=group]
group x y
1: B 1120 2.425
2: B 1140 2.425
3: B 1160 NA
4: B 1180 NA
5: B 1190 NA
6: B 1200 2.425
7: B 1200 NA
8: B 1220 2.425
9: B 1240 1.975
10: B 1260 2.450
11: B 1280 2.450
12: B 1300 1.200
13: B 1300 NA
14: B 1320 NA
15: B 1340 2.275
16: B 1360 2.375
17: B 1380 2.750
18: B 1400 2.675
19: B 1400 NA
20: B 1410 2.750
21: B 1420 3.050