# Finding Runs for Consecutive Ones

#### UserR

##### New Member
Hi,
I am new to programming. Sorry for the basic question. I came across this code to find runs for consecutive ones for the vector (1,0,0,1,1,1,0,1,1). The code for this is
Code:
findruns1<-function(x,k)
{

n<-length(x)
runs<-vector(length=n)
count<-0
for(i in 1:(n-k+1)){
if(all(x[i:(i+k-1)]==1))
{
count<-count+1
runs[count]<-i
}
}
if(count>0)
{
runs<-runs[1:count]
}else runs<-null
return(runs)
}
I am stuck at runs[count]<i
let say the question is to find the consec numbers for findruns1(y,2). The answer is 4,5,8.
Here is my level of understanding. Please correct me if i am wrong.
Initally n is 9 which is the length and i am unable to understand what is (length=n) for runs. for loops from 1 to 7 and if all[1:3]=1 which is not..the "count is count+1 and runs[count]" will never be counted for 1:3 as they are not 1 for 4:6 it will count then count will be 1 right? and what will be runs[count]<-i which i am unable to understand. please help.