Skip to content

Instantly share code, notes, and snippets.

@halfak
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halfak/7d72f05518162b5e63f6 to your computer and use it in GitHub Desktop.
Save halfak/7d72f05518162b5e63f6 to your computer and use it in GitHub Desktop.
first = function(vector, condition, start=1){
for(i in start:length(vector)){
if(condition(vector[i])){
return(i)
}
}
return(length(vector))
}
vector = c(1,2,34,3,23,2,34,5,67,78,8,8,5,4,3,23,2,4,5,6,34,23,4)
start = 1
while(start <= length(vector)){
end_of_chunk = first(vector, function(v){v>10}, start)
if(end_of_chunk > start){
print(vector[start:(end_of_chunk-1)])
}
start = end_of_chunk + 1
}
[1] 1 2
[1] 3
[1] 2
[1] 5
[1] 8 8 5 4 3
[1] 2 4 5 6
@halfak
Copy link
Author

halfak commented Sep 16, 2014

first = function(vector, condition, start=1){

  • for(i in start:length(vector)){
    
  •     if(condition(vector[i])){
    
  •         return(i)
    
  •     }
    
  • }
    
  • return(length(vector))
    
  • }

vector = c(1,2,34,3,23,2,34,5,67,78,8,8,5,4,3,23,2,4,5,6,34,23,4)
start = 1
while(start <= length(vector)){

  • end_of_chunk = first(vector, function(v){v>10}, start)
    
  • print(vector[start:end_of_chunk])
    
  • start = end_of_chunk + 1
    
  • }
    [1] 1 2 34
    [1] 3 23
    [1] 2 34
    [1] 5 67
    [1] 78
    [1] 8 8 5 4 3 23
    [1] 2 4 5 6 34
    [1] 23
    [1] 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment