Speed up a sampling function
This question is probably not appropriate for stackoverflow, since it requires mainly code review.

First of all, there is a logics problem with your while loop. Changing | to &amp; reduces the number of loops considerably.

Furthermore you should not use dataframes, if you can use a matrix.

my.sampling1&lt;-function(days, status, s.length, s.freq, iter){
    start&lt;-Sys.time()

    #build the data frame
    ddd&lt;-data.frame(days=days, positive=status)

#length of each sample
    j&lt;-s.length

#Buld the sample space
    monitor.ss&lt;-matrix(nrow=dim(ddd)[1]-j+1, ncol=j+1)
    n.row&lt;-dim(ddd)[1]-j+1

    #Create the elements of sample space: each row of the monitor.ss is an element of the sample space
    for (i in 1:j){
        monitor.ss[,i]&lt;-i:(n.row-1+i)
    }
    rm(i)


    #adds if the each of the possible samples of the sample space at least one event was observed or not
    monitor.ss[,j+1]&lt;-apply(monitor.ss,1,function(x) {
        r.low&lt;-range(x,na.rm=T)[1]
        r.high&lt;-range(x,na.rm=T)[2]
        return(as.numeric(sum(ddd$positive[r.low:r.high])&gt;0))}) 


suc.list&lt;-rep(NA, iter) #Initiate a vector to keep track of successes

    #Now the sampling takes place

    k&lt;-s.freq #number of samples drawn from the sample space
    for (i in 1:iter) { #for each iteration

        #do not convert to data.frame
        sampling.start&lt;-monitor.ss

        k.t=0 #controller to break the while loop
        suc&lt;-0

        #use &amp; to stop the loop if one condition is not fullfilled, second condition changed because a matrix is used instead of a data.frame
        while (k.t&lt;=k &amp; !is.null(nrow(sampling.start))) { #breaks the while loop if k reaches the specified limit (k=s.freq) or if the reduction of the sample space (see below) does not leave any elements in the sample space
            #set.seed(k.t*i) #use set.seed to test if results are identical
            s&lt;-sampling.start[sample(nrow(sampling.start),1),] #samples the first trial
            suc&lt;-suc+s[j+1] #adds up if there is a success or not ( a tleast one event in the sample was observed)
            sampling.start &lt;- sampling.start[apply(sampling.start, 1, function(x) !any(x %in% as.numeric(s[1:j]))),] #sample space reduction: Elements of the sample space that intersect with the sample that was just selected are removed from the sample space before the next sampling occurs
            k.t&lt;-k.t+1 #controller for the wile loop
        }

        suc.list[i]&lt;-(suc/k.t)

    }
    print(Sys.time()-start)
    message("")
    #hist(suc.list, breaks=sqrt(iter))
    return(suc.list)
}
kkk&lt;-data.frame(days=1:100,positive=rbinom(100,1,0.05)) 
res1&lt;-my.sampling1(kkk$days, kkk$positive, s.length=3, s.freq=5, iter=20)


This is already faster. To make it even more efficient, I would try to avoid apply inside the loop (could an algebraic operation be used instead?) and try to vectorize. Furthermore you can tidy up. There are some unnecessary assignments inside the function.