Sliding window in a data frame r
<p>I’m trying to obtain the proportions of individuals that that shares certain DNA sequences between two given points. And I want to use a specific sliding window. In order to show the problem I create this example. First I create a data frame with four columns.</p>

<pre><code>x&lt;-c(rep("sc256",times=2000),rep("sc784",times=2000))
pos1&lt;-round(runif(2000,100,5000),digits=0)
pos2&lt;-round(runif(2000,100,5000),digits=0)
y3&lt;-rep(c(2,1),times=2000)
M1&lt;-data.frame(x,pos1,pos2,y3)
colnames(M1)=c("iid","pos1","pos2","chr")
</code></pre>

<p>I also create a function to obtain the proportion of individuals that have sequences in a particular interval.</p>

<pre><code>roh_island&lt;-function(pop,chr,p1,p2){
a&lt;-pop[pop$chr==chr,]
island&lt;-subset(a,pos1&gt;=p1 &amp; pos2&lt;=p2)
n&lt;-nrow(island)/length(M1$iid)
return(n)
}

roh_island(M1,1,345,700)
</code></pre>

<p>Now I want to transform this interval into a sliding window of size 10 that moves between values 0 and 7000. So this window will take positions [0,10);(10,20),…,(6990,7000]. I also need that the new function with the slide window stores all the windows and proportion of individuals in each in a data frame to afterwards plot it. I try some solutions that I have found regarding sliding windows I saw but I could not make them work. Thanks</p>