I have a range of values
c(1,2,3,4,5,8,9,10,13,14,15)
And I want to find ranges where numbers get intermittent. All I want is the output:
(1,5) (8,10) (13,15)
I need to find breakpoints.
I need to do this in R.
Something like that?
x <- c(1:5, 8:10, 13:15) # example data unname(tapply(x, cumsum(c(1, diff(x)) != 1), range) # [[1]] # [1] 1 5 # # [[2]] # [1] 8 10 # # [[3]] # [1] 13 15
Another example:
x <- c(1, 5, 10, 11:14, 20:21, 23) unname(tapply(x, cumsum(c(1, diff(x)) != 1), range)) # [[1]] # [1] 1 1 # # [[2]] # [1] 5 5 # # [[3]] # [1] 10 14 # # [[4]] # [1] 20 21 # # [[5]] # [1] 23 23
x <- c(1:5, 8:10, 13:15) rr <- rle(x - seq_along(x)) rr$values <- seq_along(rr$values) s <- split(x, inverse.rle(rr)) s # $`1` # [1] 1 2 3 4 5 # # $`2` # [1] 8 9 10 # # $`3` # [1] 13 14 15 ## And then to get *literally* what you asked for: cat(paste0("(", gsub(":", ",", sapply(s, deparse)), ")"), sep="\n") # (1,5) # (8,10) # (13,15)
seqle, . cgwtools SO , .
seqle
cgwtools
, , diff/cumsum/range :
x <- c(1:5, 8:10, 13:15) x. <- c(0, cumsum( diff(x)-1 ) ) lapply( split(x, x.), range )
Source: https://habr.com/ru/post/1536683/More articles:Доступ к значениям отдельных JTextFields, созданных в цикле - javaAzure wildcard subdomains - dnsFloating-Point Hash Table - hashtableGO BACK, DO NOT SECURE - dnsHow to map a wildcard domain to an instance of a Windows Azure website? - windowsHow to define support for a "scoped" style attribute - javascriptКак сгенерирован файл "car.xml" из примера dbus, называемый "образ удаленного автомобиля с дистанционным управлением D-Bus"? - qtChange default default animation output - androidПроблема с SymbolSource Server - debugginghttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1536688/how-to-convert-images-to-gif-using-gifjs&usg=ALkJrhgMFfVc5MmM4rAfAlTDdGsnlf2FlwAll Articles