Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test whether a sequence of sections have gaps in it?

Tags:

r

I have several (ice-)core section samples in a dataset (ID in the example below). Some cores have missing sections (i.e. gaps), but I do not know which ones. How to find this out using R?

Example:

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" and "b" do not have gaps, whereas "c" does (missing piece between 20 and 30), so I am after a following result:

$a
[1] TRUE

$b
[1] TRUE

$c
[1] FALSE
like image 748
Mikko Avatar asked Feb 08 '23 17:02

Mikko


1 Answers

You can try:

lapply(split(dt,dt$ID),function(x) all(x[-1,2]==x[-nrow(x),3]))
#$a
#[1] TRUE
#$b
#[1] TRUE
#$c
#[1] FALSE
like image 199
nicola Avatar answered Feb 15 '23 18:02

nicola