How to check if section sequence has spaces?

I have several (ice) core cross-sectional samples in a dataset ( IDin the example below). Some kernels lack sections (i.e. spaces), but I don’t know which ones. How to find 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" have no spaces, while "c" does (there is no fragment between 20 and 30), so I get the following result:

$a
[1] TRUE

$b
[1] TRUE

$c
[1] FALSE
+4
source share
2 answers

You may 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
+4
source

Here's the dplyr approach:

library(dplyr)
dt %>% 
  group_by(ID) %>% 
  summarise(check = all(Sec.end == lead(Sec.start, default = last(Sec.end))))
#Source: local data table [3 x 2]
#
#      ID check
#  (fctr) (lgl)
#1      a  TRUE
#2      b  TRUE
#3      c FALSE

Or with data.table:

library(data.table)
setDT(dt)[, .(check = all(Sec.end == shift(Sec.start, 1L, 'lead', fill = last(Sec.end)))), 
               by=ID]
#   ID check
#1:  a  TRUE
#2:  b  TRUE
#3:  c FALSE

lag/lead ( , shift), Sec.end Sec.start. , Sec.start, , Sec.end - , ( ) TRUE. all, , TRUE .

+4

Source: https://habr.com/ru/post/1629587/


All Articles