Q:

Improving missing value removing code in R

I have to remove 0 or -ve values from a vector of time series. An example of a vector of time series is given below:
c(-1856, -1770, -1787, 1322, 1605, 1417, 1423, 1371, 1200, 1295, 1441,    587, 189, 330, 1133, 1398, 1455, 100, 455, 59, 222, 330, 289, 251, 1717,   1258, 1732, -3258) 

-ve values and 0 values are treated as missing values and it should be replaced by the mean of immediate neighbors (non-missing value). As for example in the vector above missing/-ve values will be replaced by the mean of (1322 and 1732).
The output will be as follows
1527, 1527, 1527, 1322, 1605, 1417, 1423, 1371, 1200, 1295, 1441,  587,  189,  330, 1133, 1398, 1455,  100,  455,   59,  222,  330,  289,  251, 1717, 1258, 1732, 1527

There may be multiple cases of missing values and edge cases should also be considered. Some of the examples are given below.
Vector ending with -ve value

x = c(1856, 1770, 1787, 1322, 1605, 1417, 1423, 1371, 1200, 1295, 441,   587, 189, 330, 1133, 1398, 1455, 100, 455, 59, 222, 330, 289, 251, 1717,   1258, 1732,-3258)

Vector with multiple missing values

x = c(1856, 1770, 1787, 1322, 1605, 1417, 1423, 1371, 1200, 1295, 441,   587, 189, 330, 1133, 1398, 1455, -100, -455, -59, -222, -330, -289, 251, 1717,   1258, 1732,-3258)

Vector starting with -ve value

x = c(-1856, 1770, 1787, 1322, 1605, 1417, 1423, 1371, 1200, 1295, 441,   587, 189, 330, 1133, 1398, 1455, 100, 455, 59, 222, 330, 289, 251, 1717,   1258, 1732,3258)

Vector with starting and ending -ve values

I have managed to do the job but my code is very slow. I have to run it on a very large data set and it is taking 2 hrs for single year. Any help to improve my code will be appreciated..
My code
drop_remo = function(x){
            if (sum(x<=0)>15){
                finalx= x[x<=0 | x>0]=0
            }else if (all(x>0)){
                finalx = x
            }else{
                newx = c(x[1:length(x)],x)
                nloc = which(newx <= 0)
                Breaks <- c(which(diff(nloc) != 1), length(nloc))
                vv = sapply(seq(length(Breaks) - 1),
                        function(n) nloc[(Breaks[n] + 1):Breaks[n+1]])

                if (is.matrix(vv)){
                    b1<-apply(vv,2,list)
                    vv = lapply(b1,unlist)
                    }
                for (i in vv){
                    #print(i)
                    backdata = newx[i[1]-1]
                    if(i[length(i)]==length(newx)){
                        smoothed = newx[length(newx)/2]
                    }else if((length(vv)==1) & (i[length(i)]==length(newx))){
                        forwarddata = newx[(i[length(i)]/2) +1]

                    }else{
                        forwarddata = newx[i[length(i)]+1]
                        smoothed = (backdata+forwarddata)/2
                    }
                    #print(backdata)    
                    #print(forwarddata)
                    #print(smoothed)
                    for (m in i){
                        newx[m] = smoothed
                    }
                 }
                finalx = newx[(length(x)+1):length(newx)]
             }
        }

A:

One way using base R, could be to get index of values where x > 0 and then take mean of values at min and max index.
inds <- which(x > 0)
x[x <=0] <- mean(c(x[min(inds)] , x[max(inds)]))

Or a one-liner using range
x[x<=0] <- mean(x[range(which(x > 0))])

For the updated data, we can use ave. Find out index which needs to be replaced (inds), create a grouping variable by using diff and cumsum and based on condition get the mean
inds <- which(x <= 0)
x[inds] <- ave(inds, cumsum(c(0, diff(inds)) != 1), FUN = function(i) {
           if((max(i) + 1) > length(x))
              mean(c(x[min(i) - 1], x[which.max(x > 0)]))
           else if ((min(i) - 1) <= 0)
              mean(c(x[max(which(x > 0))], x[(max(i) + 1)]))
           else 
              mean(c(x[min(i) - 1], x[(max(i) + 1)]))
           })

