1

Say I have a series of numbers:

seq1<-c(1:20,25:40,48:60)

How can I return a vector that lists points in which the sequence was broken, like so:

   c(21,24)
    [1] 21 24

   c(41,47) 
    [1] 41 47

Thanks for any help.

To show my miserably failing attempt:

nums<-min(seq1):max(seq1) %in% seq1
which(nums==F)[1]

res.vec<-vector()
counter<-0
res.vec2<-vector()
counter2<-0

for (i in 2:length(seq1)){
  if(nums[i]==F & nums[i-1]!=F){
    counter<-counter+1
    res.vec[counter]<-seq1[i]
  }

  if(nums[i]==T & nums[i-1]!=T){
    counter2<-counter2+1
    res.vec2[counter2]<-seq1[i]
  }


}

cbind(res.vec,res.vec2)
R-Enthusiast
  • 340
  • 1
  • 3
  • 10

2 Answers2

4

I have changed the general function a bit so I think this should be a sepparate answer.

You could try

seq1<-c(1:20,25:40,48:60)

myfun<-function(data,threshold){
   cut<-which(c(1,diff(data))>threshold)
   return(cut)
}

You get the points you have to care about using

myfun(seq1,1)
[1] 21 37

In order to better use is convenient to create an object with it.

pru<-myfun(seq1,1)

So you can now call

df<-data.frame(pos=pru,value=seq1[pru])
df
  pos value
1  21    25
2  37    48

You get a data frame with the position and the value of the brakes with your desired threshold. If you want a list instead of a data frame it works like this:

list(pos=pru,value=seq1[pru])
$pos
[1] 21 37

$value
[1] 25 48
Matias Andina
  • 4,029
  • 4
  • 26
  • 58
2

Function diff will give you the differences between successive values

> x <- c(1,2,3,5,6,3)
> diff(x)
[1] 1 1 2 1 -3

Now look for those values that are not equal to one for "breakpoints" in your sequence.

Taking in account the comments made here. For a general purpose, you could use.

fun<-function(data,threshold){
   t<-which(c(1,diff(data)) != threshold)
   return(t)
}

Consider that data could be any numerical vector (such as a data frame column). I would also consider using grep with a similar approach but it all depends on user preference.

Matias Andina
  • 4,029
  • 4
  • 26
  • 58
kasterma
  • 4,259
  • 1
  • 20
  • 27
  • I understand that the OP didn't ask for it but I think it would be awesome to have this approach with diff built into a function. Maybe something like function(sequence,threshold) that recognice a threshold jump within a sequence. – Matias Andina Jul 16 '15 at 19:12
  • 1
    This is still no where near the desired output yet. – David Arenburg Jul 16 '15 at 19:43
  • @DavidArenburg You're right, I've added my own answer to this question now – Matias Andina Jul 16 '15 at 20:20
  • @DavidArenburg quite; I think however that it is the key ingredient for a solution. I think the reader can take it from there. – kasterma Jul 17 '15 at 08:23