我在数据集中有几个(冰)核心部分样本(下面示例中的 ID)。有些核心缺少部分(即间隙),但我不知道是哪些部分。如何使用R找出这一点?

例子:

dt <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 3L), .Label = c("a", "b", "c"), class = "factor"), Sec.start = c(0,
5, 10, 20, 50, 100, 200, 0, 5, 10, 30), Sec.end = c(5, 10, 20,
30, 100, 200, 400, 5, 10, 20, 50), Section = c("0-5", "5-10",
"10-20", "20-30", "50-100", "100-200", "200-400", "0-5", "5-10",
"10-20", "30-50")), .Names = c("ID", "Sec.start", "Sec.end",
"Section"), row.names = c(NA, -11L), class = "data.frame")

dt

    ID Sec.start Sec.end Section
1   a         0       5     0-5
2   a         5      10    5-10
3   a        10      20   10-20
4   a        20      30   20-30
5   b        50     100  50-100
6   b       100     200 100-200
7   b       200     400 200-400
8   c         0       5     0-5
9   c         5      10    5-10
10  c        10      20   10-20
11  c        30      50   30-50

“a”和“b”没有间隙,而“c”有(20到30之间的缺失部分),所以我追求以下结果:
$a
[1] TRUE

$b
[1] TRUE

$c
[1] FALSE

最佳答案

你可以试试:

lapply(split(dt,dt$ID),function(x) all(x[-1,2]==x[-nrow(x),3]))
#$a
#[1] TRUE
#$b
#[1] TRUE
#$c
#[1] FALSE

关于r - 如何测试一段序列中是否有间隙?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35509282/

10-12 13:59