title: string index matching alternate 1 and -1
body: <p>I am trying to write a function that takes in a vector of integers and returns the indexes where 1 and -1 alternate. I thought this would be a simple function but it is proving devilishly hard to write! </p>
  
  <p>Example:</p>
  
  <pre><code>index:  1 2 3 4 5 6   7 8 9 10
  string: 1 0 0 0 1 -1 -1 0 1 -1 #this is input to function
  
  returns index of -1 and 1 so that -1 index is listed first 
  [6,1] #from 1 to -1
  [6,9] #from -1 to 1
  [10,9] #from 1 to -1
  </code></pre>
  
  <p>My (non-working) attempt:</p>
  
  <pre><code>x&lt;-c(1,0,0,0,1,-1,-1,0,1)
  
  matchVals(x)
  
  matchVals&lt;-function(x){
      current.index&lt;-getStart(x)
      #next VALUE to search for should be:
      next.val&lt;-x[current.index]*-1
      next.index&lt;-getNextVal(x[current.index:length(x)],next.val) + current.index #adding in offset
      return(current.index,next.index)
  }
  
  #gets index of first value that isnt a 0
  getStart&lt;-function(x){
      lapply(1:length(x),function(i){if(x[i]!=0)return(i)})
      return(NA)
  }
  
  #gets FIRST index of the value specified (so dont feed it the entire string). Must add offset for truncated portion of string
  getNextVal&lt;-function(x,v){
      lapply(1:length(x),function(i){if(x[i]==v)return(i)})
      return(NA)
  }
  </code></pre>