在 R 的列表中选择上一个和下一个值
问题描述:
我有一个列表对象,其中第一列是质量,第二列是丰度.这是一个简短的例子:
I have a list object, where the first column is mass and the second is abundance. Here a short example:
772.35 0
772.9 10
773.81 0
...
885.64 0
885.65 10
885.68 313
885.70 4455
885.78 71
885.82 0
...
889.12 0
889.13 56
885.82 0
...
900.31 0
900.34 10
901.22 1901
902.8 0
我必须选择丰度 > 100 的行作为从 0 开始到 0 结束的系列.结果将是:
I have to select rows where the abundance > 100 as a serie starting with 0 and ending with 0. The result will be:
885.64 0
885.65 10
885.68 313
885.70 4455
885.78 71
885.82 0
900.31 0
900.34 10
901.22 1901
902.8 0
该系列可能包含两个丰度 > 100 (885.68 313 and 885.70 4455)
但我的结果必须没有重复
The serie may contains two abundances > 100 (885.68 313 and 885.70 4455)
but my result must be without duplicate
数据
dd <- read.table(text = "772.35 0
772.9 10
773.81 0
885.64 0
885.65 10
885.68 313
885.70 4455
885.78 71
885.82 0
889.12 0
889.13 56
885.82 0
900.31 0
900.34 10
901.22 1901
902.8 0")
答
setDT(dd)[,group:=cumsum(c(diff(as.numeric(!V2)),0)<0)][,b:=any(V2>100),by=group][!!b]
V1 V2 group b
1: 885.64 0 2 TRUE
2: 885.65 10 2 TRUE
3: 885.68 313 2 TRUE
4: 885.70 4455 2 TRUE
5: 885.78 71 2 TRUE
6: 885.82 0 2 TRUE
7: 900.31 0 4 TRUE
8: 900.34 10 4 TRUE
9: 901.22 1901 4 TRUE
10: 902.80 0 4 TRUE