[英]Remove only initial NA values from data.table
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
.在下面的 data.table 中,我想删除所有 NA 值,直到在y
列中遇到至少两个连续的数值。 As per this condition, all rows from 1 to 9 should be removed from the table below.根据此条件,应从下表中删除从 1 到 9 的所有行。
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 -我认为解决方案从制作一组 NA 值开始,但不知道如何进一步进行 -
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)]
You could use shift
to test two consecutive values, and cumsum
to hold the selection once initial trigger condition is met:您可以使用shift
来测试两个连续的值,并在满足初始触发条件后使用cumsum
来保持选择:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.