【发布时间】:2019-04-01 03:51:29
【问题描述】:
这是我的 data.table 的一个示例(称为“sub”):
time fly mm genotype Genotype
1: 1 test 0.68000000 ctrl1_f loxP_f
2: 2 test 1.53394915 ctrl1_f loxP_f
3: 3 test 1.40431478 ctrl1_f loxP_f
4: 4 test 0.29154759 ctrl1_f loxP_f
5: 5 test 0.67416615 ctrl1_f loxP_f
6: 6 test 0.09848858 ctrl1_f loxP_f
7: 7 test 2.46099573 ctrl1_f loxP_f
8: 8 test 1.71143215 ctrl1_f loxP_f
9: 9 test 3.75767215 ctrl1_f loxP_f
10: 10 test 5.99067609 ctrl1_f loxP_f
11: 11 test 5.48714862 ctrl1_f loxP_f
12: 12 test 0.00000000 ctrl1_f loxP_f
13: 13 test 0.32015621 ctrl1_f loxP_f
14: 14 test 0.75960516 ctrl1_f loxP_f
15: 15 test 0.31953091 ctrl1_f loxP_f
16: 16 test 0.67007462 ctrl1_f loxP_f
17: 17 test 1.55467038 ctrl1_f loxP_f
18: 18 test 1.13564959 ctrl1_f loxP_f
19: 19 test 0.39051248 ctrl1_f loxP_f
20: 20 test 1.31061054 ctrl1_f loxP_f
21: 21 test 2.57007782 ctrl1_f loxP_f
22: 22 test 1.78339564 ctrl1_f loxP_f
23: 23 test 1.70484603 ctrl1_f loxP_f
24: 24 test 0.63198101 ctrl1_f loxP_f
25: 25 test 0.00000000 ctrl1_f loxP_f
26: 26 real 1.74183811 ctrl1_f loxP_f
27: 27 real 1.01000000 ctrl1_f loxP_f
28: 28 real 0.85052925 ctrl1_f loxP_f
29: 29 real 0.50000000 ctrl1_f loxP_f
30: 30 real 0.56885851 ctrl1_f loxP_f
31: 31 real 0.25000000 ctrl1_f loxP_f
32: 32 real 0.46270941 ctrl1_f loxP_f
33: 33 real 0.71000000 ctrl1_f loxP_f
34: 34 real 0.30000000 ctrl1_f loxP_f
35: 35 real 0.29410882 ctrl1_f loxP_f
36: 36 real 0.65122961 ctrl1_f loxP_f
37: 37 real 0.56435804 ctrl1_f loxP_f
38: 38 real 1.37277092 ctrl1_f loxP_f
39: 39 real 5.59322805 ctrl1_f loxP_f
40: 40 real 3.15634282 ctrl1_f loxP_f
41: 41 real 4.09078232 ctrl1_f loxP_f
42: 42 real 2.02022276 ctrl1_f loxP_f
43: 43 real 1.32196823 ctrl1_f loxP_f
44: 44 real 1.98909527 ctrl1_f loxP_f
45: 45 real 2.45985772 ctrl1_f loxP_f
46: 46 real 3.61203544 ctrl1_f loxP_f
47: 47 real 7.97250902 ctrl1_f loxP_f
48: 48 real 3.05949342 ctrl1_f loxP_f
49: 49 real 2.41754007 ctrl1_f loxP_f
50: 50 real 1.27882759 ctrl1_f loxP_f
我的目标是比较“mm”列中的连续条目并检查 0 后跟非 0 的位置,例如在上面代码的第 12 行,然后在第 25 行。结果应该保存在一个新列中。
当我这样做时,它会起作用:
sub[, initiate := lapply(1:(nrow(.SD) - 1), function(x) mm[x] == 0 && mm[x + 1] != 0)]
结果如下data.table:
time fly mm genotype Genotype initiate
1: 1 test 0.68000000 ctrl1_f loxP_f FALSE
2: 2 test 1.53394915 ctrl1_f loxP_f FALSE
3: 3 test 1.40431478 ctrl1_f loxP_f FALSE
4: 4 test 0.29154759 ctrl1_f loxP_f FALSE
5: 5 test 0.67416615 ctrl1_f loxP_f FALSE
6: 6 test 0.09848858 ctrl1_f loxP_f FALSE
7: 7 test 2.46099573 ctrl1_f loxP_f FALSE
8: 8 test 1.71143215 ctrl1_f loxP_f FALSE
9: 9 test 3.75767215 ctrl1_f loxP_f FALSE
10: 10 test 5.99067609 ctrl1_f loxP_f FALSE
11: 11 test 5.48714862 ctrl1_f loxP_f FALSE
12: 12 test 0.00000000 ctrl1_f loxP_f TRUE
13: 13 test 0.32015621 ctrl1_f loxP_f FALSE
14: 14 test 0.75960516 ctrl1_f loxP_f FALSE
15: 15 test 0.31953091 ctrl1_f loxP_f FALSE
16: 16 test 0.67007462 ctrl1_f loxP_f FALSE
17: 17 test 1.55467038 ctrl1_f loxP_f FALSE
18: 18 test 1.13564959 ctrl1_f loxP_f FALSE
19: 19 test 0.39051248 ctrl1_f loxP_f FALSE
20: 20 test 1.31061054 ctrl1_f loxP_f FALSE
21: 21 test 2.57007782 ctrl1_f loxP_f FALSE
22: 22 test 1.78339564 ctrl1_f loxP_f FALSE
23: 23 test 1.70484603 ctrl1_f loxP_f FALSE
24: 24 test 0.63198101 ctrl1_f loxP_f FALSE
25: 25 test 0.00000000 ctrl1_f loxP_f TRUE
26: 26 real 1.74183811 ctrl1_f loxP_f FALSE
27: 27 real 1.01000000 ctrl1_f loxP_f FALSE
28: 28 real 0.85052925 ctrl1_f loxP_f FALSE
29: 29 real 0.50000000 ctrl1_f loxP_f FALSE
30: 30 real 0.56885851 ctrl1_f loxP_f FALSE
31: 31 real 0.25000000 ctrl1_f loxP_f FALSE
32: 32 real 0.46270941 ctrl1_f loxP_f FALSE
33: 33 real 0.71000000 ctrl1_f loxP_f FALSE
34: 34 real 0.30000000 ctrl1_f loxP_f FALSE
35: 35 real 0.29410882 ctrl1_f loxP_f FALSE
36: 36 real 0.65122961 ctrl1_f loxP_f FALSE
37: 37 real 0.56435804 ctrl1_f loxP_f FALSE
38: 38 real 1.37277092 ctrl1_f loxP_f FALSE
39: 39 real 5.59322805 ctrl1_f loxP_f FALSE
40: 40 real 3.15634282 ctrl1_f loxP_f FALSE
41: 41 real 4.09078232 ctrl1_f loxP_f FALSE
42: 42 real 2.02022276 ctrl1_f loxP_f FALSE
43: 43 real 1.32196823 ctrl1_f loxP_f FALSE
44: 44 real 1.98909527 ctrl1_f loxP_f FALSE
45: 45 real 2.45985772 ctrl1_f loxP_f FALSE
46: 46 real 3.61203544 ctrl1_f loxP_f FALSE
47: 47 real 7.97250902 ctrl1_f loxP_f FALSE
48: 48 real 3.05949342 ctrl1_f loxP_f FALSE
49: 49 real 2.41754007 ctrl1_f loxP_f FALSE
50: 50 real 1.27882759 ctrl1_f loxP_f FALSE
这样,我正确地将第 12 行和第 25 行识别为“初始”,即作为 0 后跟非 0 的行。
我无法解决的问题是:我想做同样的事情,按“fly”列分组,即分别针对“test”和“real”条目。当我这样做时,它并没有给我正确的答案:
sub[, initiate2 := lapply(1:(nrow(.SD) - 1), function(x) mm[x] == 0 && mm[x + 1] != 0), by = fly]
sub
time fly mm genotype Genotype initiate initiate2
1: 1 test 0.68000000 ctrl1_f loxP_f FALSE FALSE
2: 2 test 1.53394915 ctrl1_f loxP_f FALSE FALSE
3: 3 test 1.40431478 ctrl1_f loxP_f FALSE FALSE
4: 4 test 0.29154759 ctrl1_f loxP_f FALSE FALSE
5: 5 test 0.67416615 ctrl1_f loxP_f FALSE FALSE
6: 6 test 0.09848858 ctrl1_f loxP_f FALSE FALSE
7: 7 test 2.46099573 ctrl1_f loxP_f FALSE FALSE
8: 8 test 1.71143215 ctrl1_f loxP_f FALSE FALSE
9: 9 test 3.75767215 ctrl1_f loxP_f FALSE FALSE
10: 10 test 5.99067609 ctrl1_f loxP_f FALSE FALSE
11: 11 test 5.48714862 ctrl1_f loxP_f FALSE FALSE
12: 12 test 0.00000000 ctrl1_f loxP_f TRUE FALSE
13: 13 test 0.32015621 ctrl1_f loxP_f FALSE FALSE
14: 14 test 0.75960516 ctrl1_f loxP_f FALSE FALSE
15: 15 test 0.31953091 ctrl1_f loxP_f FALSE FALSE
16: 16 test 0.67007462 ctrl1_f loxP_f FALSE FALSE
17: 17 test 1.55467038 ctrl1_f loxP_f FALSE FALSE
18: 18 test 1.13564959 ctrl1_f loxP_f FALSE FALSE
19: 19 test 0.39051248 ctrl1_f loxP_f FALSE FALSE
20: 20 test 1.31061054 ctrl1_f loxP_f FALSE FALSE
21: 21 test 2.57007782 ctrl1_f loxP_f FALSE FALSE
22: 22 test 1.78339564 ctrl1_f loxP_f FALSE FALSE
23: 23 test 1.70484603 ctrl1_f loxP_f FALSE FALSE
24: 24 test 0.63198101 ctrl1_f loxP_f FALSE FALSE
25: 25 test 0.00000000 ctrl1_f loxP_f TRUE FALSE
26: 26 real 1.74183811 ctrl1_f loxP_f FALSE FALSE
27: 27 real 1.01000000 ctrl1_f loxP_f FALSE FALSE
28: 28 real 0.85052925 ctrl1_f loxP_f FALSE FALSE
29: 29 real 0.50000000 ctrl1_f loxP_f FALSE FALSE
30: 30 real 0.56885851 ctrl1_f loxP_f FALSE FALSE
31: 31 real 0.25000000 ctrl1_f loxP_f FALSE FALSE
32: 32 real 0.46270941 ctrl1_f loxP_f FALSE FALSE
33: 33 real 0.71000000 ctrl1_f loxP_f FALSE FALSE
34: 34 real 0.30000000 ctrl1_f loxP_f FALSE FALSE
35: 35 real 0.29410882 ctrl1_f loxP_f FALSE FALSE
36: 36 real 0.65122961 ctrl1_f loxP_f FALSE FALSE
37: 37 real 0.56435804 ctrl1_f loxP_f FALSE FALSE
38: 38 real 1.37277092 ctrl1_f loxP_f FALSE FALSE
39: 39 real 5.59322805 ctrl1_f loxP_f FALSE FALSE
40: 40 real 3.15634282 ctrl1_f loxP_f FALSE FALSE
41: 41 real 4.09078232 ctrl1_f loxP_f FALSE FALSE
42: 42 real 2.02022276 ctrl1_f loxP_f FALSE FALSE
43: 43 real 1.32196823 ctrl1_f loxP_f FALSE FALSE
44: 44 real 1.98909527 ctrl1_f loxP_f FALSE FALSE
45: 45 real 2.45985772 ctrl1_f loxP_f FALSE FALSE
46: 46 real 3.61203544 ctrl1_f loxP_f FALSE FALSE
47: 47 real 7.97250902 ctrl1_f loxP_f FALSE FALSE
48: 48 real 3.05949342 ctrl1_f loxP_f FALSE FALSE
49: 49 real 2.41754007 ctrl1_f loxP_f FALSE FALSE
50: 50 real 1.27882759 ctrl1_f loxP_f FALSE FALSE
initiate2 第 12 行和第 25 行都给我 FALSE,虽然它应该给我 TRUE 第 12 行和第 25 行 FALSE。为什么会发生这种情况,我该如何解决?我是 data.table 的新手,所以我可能缺少一些明显的东西。
【问题讨论】:
标签: r data.table